{"thread":{"id":"66350","subject":"[BUG] reference-transaction hook misses destination of git branch -m","startedAt":"2026-09-19T13:33:57Z","lastAt":"2026-10-10T00:22:07Z","messageCount":35,"participants":["Maciej Ciemborowicz","Karthik Nayak","Junio C Hamano","Patrick Steinhardt","Kristoffer Haugsbakk","brian m. carlson"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"552876","messageId":"CACQ=SRHCOCcmVCgHqd+sjMsZ9LCdSHuXdCo0gkwxXwYgF7iwig@mail.gmail.com","threadId":"66350","inReplyTo":null,"subject":"[BUG] reference-transaction hook misses destination of git branch -m","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-09-19T13:33:44Z","receivedAt":"2026-09-19T13:33:57Z","isPatch":false,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"Hello,\n\n`git branch -m old new` changes refs/heads/old into refs/heads/new,\nbut the reference-transaction hook does not report both ref changes.\n\nObserved with Git 2.55:\n\n* files backend: the hook reports deletion of refs/heads/old, but\n  does not report creation of refs/heads/new;\n* reftable backend: the hook reports no usable rename payload.\n\nThe equivalent atomic operation performed with `git update-ref --stdin`\nreports both updates correctly.\n\nI expected the transaction to include:\n\n    <oid> <zero> refs/heads/old\n    <zero> <oid> refs/heads/new\n\nMinimal reproducer:\n\n    #!/bin/sh\n    set -eu\n\n    format=${1:-files}\n    root=$(mktemp -d)\n    trap 'rm -rf \"$root\"' EXIT\n\n    repo=$root/repo\n    hooks=$root/hooks\n    log=$root/transactions\n\n    git init -q --ref-format=\"$format\" \"$repo\"\n    git -C \"$repo\" config user.name Reproducer\n    git -C \"$repo\" config user.email repro@example.com\n    git -C \"$repo\" commit --allow-empty -qm initial\n    git -C \"$repo\" branch old\n\n    mkdir \"$hooks\"\n    cat >\"$hooks/reference-transaction\" <<'HOOK'\n    #!/bin/sh\n    printf '%s\\n' \"--- $1\" >>\"$HOOK_LOG\"\n    cat >>\"$HOOK_LOG\"\n    HOOK\n    chmod +x \"$hooks/reference-transaction\"\n\n    git -C \"$repo\" config core.hooksPath \"$hooks\"\n    export HOOK_LOG=$log\n    : >\"$log\"\n\n    git -C \"$repo\" branch -m old new\n    cat \"$log\"\n\nThe behavior was also tested across Git 2.28–2.55:\nhttps://github.com/ciembor/git-hooks-ext/blob/v0.2.0/tests/compat/README.md\n\nThe reference-transaction documentation says that the hook is invoked by\nGit commands performing reference updates. A branch rename changes two refs,\nbut the destination update is not visible to the hook.\n\nThanks,\nMaciej Ciemborowicz\n"},{"id":"552899","messageId":"CAOLa=ZTN1TU2A1sgEhiw=ymMYr6Ge11cMEubSaeKqr4WNU=2EQ@mail.gmail.com","threadId":"66350","inReplyTo":"CACQ=SRHCOCcmVCgHqd+sjMsZ9LCdSHuXdCo0gkwxXwYgF7iwig@mail.gmail.com","subject":"Re: [BUG] reference-transaction hook misses destination of git branch -m","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-09-19T20:52:22Z","receivedAt":"2026-09-19T20:52:24Z","isPatch":false,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com> writes:\n\n> Hello,\n>\n> `git branch -m old new` changes refs/heads/old into refs/heads/new,\n> but the reference-transaction hook does not report both ref changes.\n>\n\nMy understanding without digging too deep is that both 'copy' and\n'rename' do not use a transaction, as such they don't trigger the\n'reference-transaction' hook. This is definitely absurd, since we would\nexpect that all reference operations should use the transaction and\ntrigger the hook.\n\n> Observed with Git 2.55:\n>\n> * files backend: the hook reports deletion of refs/heads/old, but\n>   does not report creation of refs/heads/new;\n\nYeah, because the files backend calls `refs_delete_ref()` on the old\nreference which is done within a transaction.\n\n> * reftable backend: the hook reports no usable rename payload.\n\nReftable doesn't call `refs_delete_ref()` and simply writes a TOMBSTONE\nentry to delete the old reference.\n\n> The equivalent atomic operation performed with `git update-ref --stdin`\n> reports both updates correctly.\n>\n\nBy equivalent atomic operation do you a 'delete' and 'create' command?\n\n> I expected the transaction to include:\n>\n>     <oid> <zero> refs/heads/old\n>     <zero> <oid> refs/heads/new\n>\n> Minimal reproducer:\n>\n>     #!/bin/sh\n>     set -eu\n>\n>     format=${1:-files}\n>     root=$(mktemp -d)\n>     trap 'rm -rf \"$root\"' EXIT\n>\n>     repo=$root/repo\n>     hooks=$root/hooks\n>     log=$root/transactions\n>\n>     git init -q --ref-format=\"$format\" \"$repo\"\n>     git -C \"$repo\" config user.name Reproducer\n>     git -C \"$repo\" config user.email repro@example.com\n>     git -C \"$repo\" commit --allow-empty -qm initial\n>     git -C \"$repo\" branch old\n>\n>     mkdir \"$hooks\"\n>     cat >\"$hooks/reference-transaction\" <<'HOOK'\n>     #!/bin/sh\n>     printf '%s\\n' \"--- $1\" >>\"$HOOK_LOG\"\n>     cat >>\"$HOOK_LOG\"\n>     HOOK\n>     chmod +x \"$hooks/reference-transaction\"\n>\n>     git -C \"$repo\" config core.hooksPath \"$hooks\"\n>     export HOOK_LOG=$log\n>     : >\"$log\"\n>\n>     git -C \"$repo\" branch -m old new\n>     cat \"$log\"\n>\n> The behavior was also tested across Git 2.28–2.55:\n> https://github.com/ciembor/git-hooks-ext/blob/v0.2.0/tests/compat/README.md\n>\n> The reference-transaction documentation says that the hook is invoked by\n> Git commands performing reference updates. A branch rename changes two refs,\n> but the destination update is not visible to the hook.\n>\n\nEither ways, I think we should fix this, I could have a look if you\naren't submitting a patch yourself :)\n\n> Thanks,\n> Maciej Ciemborowicz\n"},{"id":"552913","messageId":"20260920165037.88524-1-maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"CAOLa=ZTN1TU2A1sgEhiw=ymMYr6Ge11cMEubSaeKqr4WNU=2EQ@mail.gmail.com","subject":"[PATCH] refs: run copy and rename through transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-09-20T16:50:37Z","receivedAt":"2026-09-20T16:50:41Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"Reference copy and rename operations currently bypass the transaction API.\nConsequently, the reference-transaction hook sees only the source deletion\nwith the files backend and no useful update with the reftable backend.\n\nRepresent both operations as reference transactions containing their\nlogical updates. A rename is a deletion of the old reference and creation\nof the new reference in the same transaction. Retain backend-specific\nreflog handling: the files backend stages its existing rename procedure\nacross prepare, finish and abort, while reftable stages an addition while\nholding the stack lock. Suppress hooks for the files backend's nested\ndeletion transactions so that callers observe one logical transaction.\n\nRecord and verify the source and destination values after taking backend\nlocks. This rejects concurrent changes instead of applying a rename or copy\nthat differs from the payload shown to the preparing hook. Preserve D/F\nrenames and restore overwritten references and reflogs when a prepared hook\nrejects the operation.\n\nAdd coverage for rename, copy, forced updates, both directions of D/F\nconflicts, concurrent updates and prepared-hook rollback.\n\nHelped-by: Karthik Nayak <karthik.188@gmail.com>\nSigned-off-by: Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com>\n---\n refs.c                           | 137 ++++++++++++---\n refs.h                           |   3 +\n refs/debug.c                     |  25 ---\n refs/files-backend.c             | 276 +++++++++++++++++++++++++++----\n refs/packed-backend.c            |   2 -\n refs/refs-internal.h             |  38 +++--\n refs/reftable-backend.c          | 194 ++++++++++++++++------\n t/t1416-ref-transaction-hooks.sh | 142 ++++++++++++++++\n 8 files changed, 679 insertions(+), 138 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 92d5df5b7..22c000f7f 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1004,15 +1004,17 @@ long get_files_ref_lock_timeout_ms(struct repository *repo)\n \treturn timeout_ms;\n }\n \n-int refs_delete_ref(struct ref_store *refs, const char *msg,\n-\t\t    const char *refname,\n-\t\t    const struct object_id *old_oid,\n-\t\t    unsigned int flags)\n+int refs_delete_ref_with_transaction_flags(struct ref_store *refs,\n+\t\t\t\t\t   const char *msg,\n+\t\t\t\t\t   const char *refname,\n+\t\t\t\t\t   const struct object_id *old_oid,\n+\t\t\t\t\t   unsigned int flags,\n+\t\t\t\t\t   unsigned int transaction_flags)\n {\n \tstruct ref_transaction *transaction;\n \tstruct strbuf err = STRBUF_INIT;\n \n-\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n+\ttransaction = ref_store_transaction_begin(refs, transaction_flags, &err);\n \tif (!transaction ||\n \t    ref_transaction_delete(transaction, refname, old_oid,\n \t\t\t\t   NULL, flags, msg, &err) ||\n@@ -1027,6 +1029,15 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,\n \treturn 0;\n }\n \n+int refs_delete_ref(struct ref_store *refs, const char *msg,\n+\t\t    const char *refname,\n+\t\t    const struct object_id *old_oid,\n+\t\t    unsigned int flags)\n+{\n+\treturn refs_delete_ref_with_transaction_flags(refs, msg, refname,\n+\t\t\t\t\t\t      old_oid, flags, 0);\n+}\n+\n static void copy_reflog_msg(struct strbuf *sb, const char *msg)\n {\n \tchar c;\n@@ -1270,6 +1281,10 @@ void ref_transaction_free(struct ref_transaction *transaction)\n \n \tstring_list_clear(&transaction->refnames, 0);\n \tfree(transaction->updates);\n+\tfree(transaction->old_refname);\n+\tfree(transaction->new_refname);\n+\tfree(transaction->logmsg);\n+\tfree(transaction->destination_target);\n \tfree(transaction);\n }\n \n@@ -2710,7 +2725,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,\n \t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \n \t/* Preparing checks before locking references */\n-\tret = run_transaction_hook(transaction, \"preparing\");\n+\tret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :\n+\t\trun_transaction_hook(transaction, \"preparing\");\n \tif (ret) {\n \t\tref_transaction_abort(transaction, err);\n \t\tdie(_(abort_by_ref_transaction_hook), \"preparing\");\n@@ -2720,7 +2736,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,\n \tif (ret)\n \t\treturn ret;\n \n-\tret = run_transaction_hook(transaction, \"prepared\");\n+\tret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :\n+\t\trun_transaction_hook(transaction, \"prepared\");\n \tif (ret) {\n \t\tref_transaction_abort(transaction, err);\n \t\tdie(_(abort_by_ref_transaction_hook), \"prepared\");\n@@ -2750,7 +2767,8 @@ int ref_transaction_abort(struct ref_transaction *transaction,\n \t\tbreak;\n \t}\n \n-\trun_transaction_hook(transaction, \"aborted\");\n+\tif (!(transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK))\n+\t\trun_transaction_hook(transaction, \"aborted\");\n \n \tref_transaction_free(transaction);\n \treturn ret;\n@@ -2781,7 +2799,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t}\n \n \tret = refs->be->transaction_finish(refs, transaction, err);\n-\tif (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL))\n+\tif (!ret && !(transaction->flags & (REF_TRANSACTION_FLAG_INITIAL |\n+\t\t\t\t\t REF_TRANSACTION_FLAG_SKIP_HOOK)))\n \t\trun_transaction_hook(transaction, \"committed\");\n \treturn ret;\n }\n@@ -3123,28 +3142,100 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n \treturn ret;\n }\n \n-int refs_rename_ref(struct ref_store *refs, const char *oldref,\n-\t\t    const char *newref, const char *logmsg)\n+static int refs_copy_or_rename_ref(struct ref_store *refs, const char *oldref,\n+\t\t\t\t   const char *newref, const char *logmsg,\n+\t\t\t\t   int copy)\n {\n-\tchar *msg;\n-\tint retval;\n+\tstruct ref_transaction *transaction = NULL;\n+\tstruct object_id old_oid, new_oid;\n+\tstruct strbuf new_target = STRBUF_INIT;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tchar *msg = normalize_reflog_message(logmsg);\n+\tint old_flags, new_flags = 0, new_exists = 0, ret = 1;\n \n-\tmsg = normalize_reflog_message(logmsg);\n-\tretval = refs->be->rename_ref(refs, oldref, newref, msg);\n+\tif (!strcmp(oldref, newref)) {\n+\t\tret = 0;\n+\t\tgoto out;\n+\t}\n+\n+\tif (!refs_resolve_ref_unsafe(refs, oldref,\n+\t\t\t\t     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t     &old_oid, &old_flags)) {\n+\t\terror(\"refname %s not found\", oldref);\n+\t\tgoto out;\n+\t}\n+\tif (old_flags & REF_ISSYMREF) {\n+\t\terror(\"refname %s is a symbolic ref, %s it is not supported\",\n+\t\t      oldref, copy ? \"copying\" : \"renaming\");\n+\t\tgoto out;\n+\t}\n+\n+\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n+\tif (!transaction)\n+\t\tgoto error;\n+\ttransaction->type = copy ? REF_TRANSACTION_TYPE_COPY :\n+\t\tREF_TRANSACTION_TYPE_RENAME;\n+\ttransaction->old_refname = xstrdup(oldref);\n+\ttransaction->new_refname = xstrdup(newref);\n+\ttransaction->logmsg = xstrdup(msg);\n+\toidcpy(&transaction->source_oid, &old_oid);\n+\n+\tif (!copy && ref_transaction_delete(transaction, oldref, &old_oid, NULL,\n+\t\t\t\t\t    REF_NO_DEREF, msg, &err))\n+\t\tgoto error;\n+\n+\tif (refs_resolve_ref_unsafe(refs, newref,\n+\t\t\t\t    RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t    &new_oid, &new_flags)) {\n+\t\tnew_exists = 1;\n+\t\tif ((new_flags & REF_ISSYMREF) &&\n+\t\t    refs_read_symbolic_ref(refs, newref, &new_target) < 0) {\n+\t\t\tstrbuf_addf(&err, \"unable to read symbolic ref %s\", newref);\n+\t\t\tgoto error;\n+\t\t}\n+\t} else {\n+\t\toidclr(&new_oid, refs->repo->hash_algo);\n+\t}\n+\ttransaction->destination_exists = new_exists;\n+\tif (new_flags & REF_ISSYMREF)\n+\t\ttransaction->destination_target = xstrdup(new_target.buf);\n+\telse if (transaction->destination_exists)\n+\t\toidcpy(&transaction->destination_oid, &new_oid);\n+\n+\tif (ref_transaction_update(transaction, newref, &old_oid,\n+\t\t\t\t   (new_flags & REF_ISSYMREF) ? NULL : &new_oid,\n+\t\t\t\t   NULL,\n+\t\t\t\t   (new_flags & REF_ISSYMREF) ? new_target.buf : NULL,\n+\t\t\t\t   REF_NO_DEREF | REF_SKIP_CREATE_REFLOG,\n+\t\t\t\t   NULL, &err))\n+\t\tgoto error;\n+\n+\tif (ref_transaction_commit(transaction, &err))\n+\t\tgoto error;\n+\n+\tret = 0;\n+\tgoto out;\n+\n+error:\n+\terror(\"%s\", err.buf);\n+out:\n+\tref_transaction_free(transaction);\n+\tstrbuf_release(&new_target);\n+\tstrbuf_release(&err);\n \tfree(msg);\n-\treturn retval;\n+\treturn ret;\n }\n \n-int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,\n+int refs_rename_ref(struct ref_store *refs, const char *oldref,\n \t\t    const char *newref, const char *logmsg)\n {\n-\tchar *msg;\n-\tint retval;\n+\treturn refs_copy_or_rename_ref(refs, oldref, newref, logmsg, 0);\n+}\n \n-\tmsg = normalize_reflog_message(logmsg);\n-\tretval = refs->be->copy_ref(refs, oldref, newref, msg);\n-\tfree(msg);\n-\treturn retval;\n+int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,\n+\t\t    const char *newref, const char *logmsg)\n+{\n+\treturn refs_copy_or_rename_ref(refs, oldref, newref, logmsg, 1);\n }\n \n const char *ref_update_original_update_refname(struct ref_update *update)\ndiff --git a/refs.h b/refs.h\nindex 9979446d1..25bb8720f 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -784,6 +784,9 @@ enum ref_transaction_flag {\n \t * while rejecting updates which do not match the expected state.\n \t */\n \tREF_TRANSACTION_ALLOW_FAILURE = (1 << 1),\n+\n+\t/* Suppress hooks for an update nested in another transaction. */\n+\tREF_TRANSACTION_FLAG_SKIP_HOOK = (1 << 2),\n };\n \n /*\ndiff --git a/refs/debug.c b/refs/debug.c\nindex 639db0f26..87b84e767 100644\n--- a/refs/debug.c\n+++ b/refs/debug.c\n@@ -143,28 +143,6 @@ static int debug_optimize_required(struct ref_store *ref_store,\n \treturn res;\n }\n \n-static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,\n-\t\t\t    const char *newref, const char *logmsg)\n-{\n-\tstruct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;\n-\tint res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref,\n-\t\t\t\t\t      logmsg);\n-\ttrace_printf_key(&trace_refs, \"rename_ref: %s -> %s \\\"%s\\\": %d\\n\", oldref, newref,\n-\t\tlogmsg, res);\n-\treturn res;\n-}\n-\n-static int debug_copy_ref(struct ref_store *ref_store, const char *oldref,\n-\t\t\t  const char *newref, const char *logmsg)\n-{\n-\tstruct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;\n-\tint res =\n-\t\tdrefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg);\n-\ttrace_printf_key(&trace_refs, \"copy_ref: %s -> %s \\\"%s\\\": %d\\n\", oldref, newref,\n-\t\tlogmsg, res);\n-\treturn res;\n-}\n-\n struct debug_ref_iterator {\n \tstruct ref_iterator base;\n \tstruct ref_iterator *iter;\n@@ -453,9 +431,6 @@ struct ref_storage_be refs_be_debug = {\n \t.optimize = debug_optimize,\n \t.optimize_required = debug_optimize_required,\n \n-\t.rename_ref = debug_rename_ref,\n-\t.copy_ref = debug_copy_ref,\n-\n \t.iterator_begin = debug_ref_iterator_begin,\n \t.read_raw_ref = debug_read_raw_ref,\n \t.read_symbolic_ref = debug_read_symbolic_ref,\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 71628550f..92601b5e9 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1594,6 +1594,7 @@ static int files_optimize_required(struct ref_store *ref_store,\n  * live into logs/refs.\n  */\n #define TMP_RENAMED_LOG  \"refs/.tmp-renamed-log\"\n+#define TMP_RENAMED_LOG_DESTINATION \"refs/.tmp-renamed-log-destination\"\n \n struct rename_cb {\n \tconst char *tmp_renamed_log;\n@@ -1685,12 +1686,24 @@ static int refs_rename_ref_available(struct ref_store *refs,\n \treturn ok;\n }\n \n+struct files_copy_or_rename_transaction_data {\n+\tstruct ref_lock *lock;\n+\tstruct object_id orig_oid;\n+\tstruct object_id destination_oid;\n+\tchar *destination_target;\n+\tint logmoved;\n+\tint destination_exists;\n+\tint destination_log_backed_up;\n+};\n+\n static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \t\t\t    const char *oldrefname, const char *newrefname,\n-\t\t\t    const char *logmsg, int copy)\n+\t\t\t    const char *logmsg, int copy,\n+\t\t\t    struct ref_transaction *transaction)\n {\n \tstruct files_ref_store *refs =\n-\t\tfiles_downcast(ref_store, REF_STORE_WRITE, \"rename_ref\");\n+\t\tfiles_downcast(ref_store, REF_STORE_WRITE,\n+\t\t\t       \"ref_transaction_prepare\");\n \tstruct object_id orig_oid;\n \tint flag = 0, logmoved = 0;\n \tstruct ref_lock *lock;\n@@ -1698,12 +1711,19 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \tstruct strbuf sb_oldref = STRBUF_INIT;\n \tstruct strbuf sb_newref = STRBUF_INIT;\n \tstruct strbuf tmp_renamed_log = STRBUF_INIT;\n+\tstruct strbuf tmp_destination_log = STRBUF_INIT;\n+\tstruct strbuf destination_target = STRBUF_INIT;\n \tint log, ret;\n+\tint destination_exists = 0, destination_flags = 0;\n+\tint destination_log_backed_up = 0;\n+\tstruct object_id destination_oid;\n+\tstruct files_copy_or_rename_transaction_data *data;\n \tstruct strbuf err = STRBUF_INIT;\n \n \tfiles_reflog_path(refs, &sb_oldref, oldrefname);\n \tfiles_reflog_path(refs, &sb_newref, newrefname);\n \tfiles_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);\n+\tfiles_reflog_path(refs, &tmp_destination_log, TMP_RENAMED_LOG_DESTINATION);\n \n \tlog = !lstat(sb_oldref.buf, &loginfo);\n \tif (log && S_ISLNK(loginfo.st_mode)) {\n@@ -1727,11 +1747,67 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \t\t\t\t    oldrefname);\n \t\tgoto out;\n \t}\n+\tif (!oideq(&orig_oid, &transaction->source_oid)) {\n+\t\tret = error(\"refname %s is at %s but expected %s\",\n+\t\t\t    oldrefname, oid_to_hex(&orig_oid),\n+\t\t\t    oid_to_hex(&transaction->source_oid));\n+\t\tgoto out;\n+\t}\n \tif (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {\n \t\tret = 1;\n \t\tgoto out;\n \t}\n \n+\tif (refs_resolve_ref_unsafe(&refs->base, newrefname,\n+\t\t\t\t    RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t    &destination_oid, &destination_flags)) {\n+\t\tdestination_exists = 1;\n+\t\tif ((destination_flags & REF_ISSYMREF) &&\n+\t\t    refs_read_symbolic_ref(&refs->base, newrefname,\n+\t\t\t\t\t   &destination_target) < 0) {\n+\t\t\tret = error(\"unable to read symbolic ref %s\", newrefname);\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\tif (destination_exists != transaction->destination_exists) {\n+\t\tret = error(\"refname %s changed while renaming\", newrefname);\n+\t\tgoto out;\n+\t}\n+\tif (destination_exists) {\n+\t\tif (destination_flags & REF_ISSYMREF) {\n+\t\t\tif (!transaction->destination_target ||\n+\t\t\t    strcmp(destination_target.buf,\n+\t\t\t\t   transaction->destination_target)) {\n+\t\t\t\tret = error(\"refname %s changed while renaming\",\n+\t\t\t\t\t    newrefname);\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t} else if (transaction->destination_target ||\n+\t\t\t   !oideq(&destination_oid,\n+\t\t\t\t  &transaction->destination_oid)) {\n+\t\t\tret = error(\"refname %s changed while renaming\", newrefname);\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tif (!lstat(sb_newref.buf, &loginfo)) {\n+\t\tif (S_ISLNK(loginfo.st_mode)) {\n+\t\t\tret = error(\"reflog for %s is a symlink\", newrefname);\n+\t\t\tgoto out;\n+\t\t}\n+\t\tif (S_ISREG(loginfo.st_mode)) {\n+\t\t\tif (copy_file(refs->base.repo, tmp_destination_log.buf,\n+\t\t\t\t      sb_newref.buf, 0644)) {\n+\t\t\t\tif (errno != EEXIST)\n+\t\t\t\t\tunlink(tmp_destination_log.buf);\n+\t\t\t\tret = error(\"unable to back up logfile logs/%s: %s\",\n+\t\t\t\t\t    newrefname, strerror(errno));\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t\tdestination_log_backed_up = 1;\n+\t\t}\n+\t}\n+\n \tif (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {\n \t\tret = error(\"unable to move logfile logs/%s to logs/\"TMP_RENAMED_LOG\": %s\",\n \t\t\t    oldrefname, strerror(errno));\n@@ -1744,8 +1820,10 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \t\tgoto out;\n \t}\n \n-\tif (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,\n-\t\t\t    &orig_oid, REF_NO_DEREF)) {\n+\tif (!copy && refs_delete_ref_with_transaction_flags(&refs->base, logmsg,\n+\t\t\t\t\t\t\t oldrefname, &orig_oid,\n+\t\t\t\t\t\t\t REF_NO_DEREF,\n+\t\t\t\t\t\t\t REF_TRANSACTION_FLAG_SKIP_HOOK)) {\n \t\terror(\"unable to delete old %s\", oldrefname);\n \t\tgoto rollback;\n \t}\n@@ -1760,8 +1838,9 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \tif (!copy && refs_resolve_ref_unsafe(&refs->base, newrefname,\n \t\t\t\t\t     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n \t\t\t\t\t     NULL, NULL) &&\n-\t    refs_delete_ref(&refs->base, NULL, newrefname,\n-\t\t\t    NULL, REF_NO_DEREF)) {\n+\t    refs_delete_ref_with_transaction_flags(&refs->base, NULL, newrefname,\n+\t\t\t\t\t\t     NULL, REF_NO_DEREF,\n+\t\t\t\t\t\t     REF_TRANSACTION_FLAG_SKIP_HOOK)) {\n \t\tif (errno == EISDIR) {\n \t\t\tstruct strbuf path = STRBUF_INIT;\n \t\t\tint result;\n@@ -1796,13 +1875,25 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \t}\n \toidcpy(&lock->old_oid, &orig_oid);\n \n-\tif (write_ref_to_lockfile(refs, lock, &orig_oid, &err) ||\n-\t    commit_ref_update(refs, lock, &orig_oid, logmsg, 0, &err)) {\n+\tif (write_ref_to_lockfile(refs, lock, &orig_oid, &err)) {\n \t\terror(\"unable to write current sha1 into %s: %s\", newrefname, err.buf);\n \t\tstrbuf_release(&err);\n \t\tgoto rollback;\n \t}\n \n+\tCALLOC_ARRAY(data, 1);\n+\tdata->lock = lock;\n+\toidcpy(&data->orig_oid, &orig_oid);\n+\tdata->logmoved = logmoved;\n+\tdata->destination_exists = destination_exists;\n+\tdata->destination_log_backed_up = destination_log_backed_up;\n+\tif (destination_exists && !(destination_flags & REF_ISSYMREF))\n+\t\toidcpy(&data->destination_oid, &destination_oid);\n+\tif (destination_flags & REF_ISSYMREF)\n+\t\tdata->destination_target = strbuf_detach(&destination_target, NULL);\n+\ttransaction->backend_data = data;\n+\ttransaction->state = REF_TRANSACTION_PREPARED;\n+\n \tret = 0;\n \tgoto out;\n \n@@ -1821,38 +1912,40 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \t}\n \n  rollbacklog:\n-\tif (logmoved && rename(sb_newref.buf, sb_oldref.buf))\n-\t\terror(\"unable to restore logfile %s from %s: %s\",\n-\t\t\toldrefname, newrefname, strerror(errno));\n+\tif (logmoved) {\n+\t\tif (rename(sb_newref.buf, tmp_renamed_log.buf)) {\n+\t\t\terror(\"unable to restore logfile %s from %s: %s\",\n+\t\t\t      oldrefname, newrefname, strerror(errno));\n+\t\t} else {\n+\t\t\ttry_remove_empty_parents(refs, newrefname,\n+\t\t\t\t\t\t REMOVE_EMPTY_PARENTS_REFLOG);\n+\t\t\tif (rename_tmp_log(refs, oldrefname))\n+\t\t\t\terror(\"unable to restore logfile %s from logs/\"\n+\t\t\t\t      TMP_RENAMED_LOG \": %s\",\n+\t\t\t\t      oldrefname, strerror(errno));\n+\t\t}\n+\t}\n \tif (!logmoved && log &&\n \t    rename(tmp_renamed_log.buf, sb_oldref.buf))\n \t\terror(\"unable to restore logfile %s from logs/\"TMP_RENAMED_LOG\": %s\",\n \t\t\toldrefname, strerror(errno));\n+\tif (destination_log_backed_up &&\n+\t    rename(tmp_destination_log.buf, sb_newref.buf))\n+\t\terror(\"unable to restore logfile %s: %s\",\n+\t\t      newrefname, strerror(errno));\n \tret = 1;\n  out:\n+\tif (ret && destination_log_backed_up)\n+\t\tunlink(tmp_destination_log.buf);\n \tstrbuf_release(&sb_newref);\n \tstrbuf_release(&sb_oldref);\n \tstrbuf_release(&tmp_renamed_log);\n+\tstrbuf_release(&tmp_destination_log);\n+\tstrbuf_release(&destination_target);\n \n \treturn ret;\n }\n \n-static int files_rename_ref(struct ref_store *ref_store,\n-\t\t\t    const char *oldrefname, const char *newrefname,\n-\t\t\t    const char *logmsg)\n-{\n-\treturn files_copy_or_rename_ref(ref_store, oldrefname,\n-\t\t\t\t newrefname, logmsg, 0);\n-}\n-\n-static int files_copy_ref(struct ref_store *ref_store,\n-\t\t\t    const char *oldrefname, const char *newrefname,\n-\t\t\t    const char *logmsg)\n-{\n-\treturn files_copy_or_rename_ref(ref_store, oldrefname,\n-\t\t\t\t newrefname, logmsg, 1);\n-}\n-\n static int close_ref_gently(struct ref_lock *lock)\n {\n \tif (close_lock_file_gently(&lock->lk))\n@@ -2962,6 +3055,13 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \tstruct ref_transaction *packed_transaction = NULL;\n \n \tassert(err);\n+\tif (transaction->type != REF_TRANSACTION_TYPE_NORMAL)\n+\t\treturn files_copy_or_rename_ref(ref_store,\n+\t\t\t\ttransaction->old_refname,\n+\t\t\t\ttransaction->new_refname,\n+\t\t\t\ttransaction->logmsg,\n+\t\t\t\ttransaction->type == REF_TRANSACTION_TYPE_COPY,\n+\t\t\t\ttransaction);\n \n \tif (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n \t\tgoto cleanup;\n@@ -3318,6 +3418,10 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \treturn ret;\n }\n \n+static int files_transaction_abort(struct ref_store *ref_store,\n+\t\t\t\t   struct ref_transaction *transaction,\n+\t\t\t\t   struct strbuf *err);\n+\n static int files_transaction_finish(struct ref_store *ref_store,\n \t\t\t\t    struct ref_transaction *transaction,\n \t\t\t\t    struct strbuf *err)\n@@ -3333,6 +3437,33 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \n \n \tassert(err);\n+\tif (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {\n+\t\tstruct files_copy_or_rename_transaction_data *data =\n+\t\t\ttransaction->backend_data;\n+\t\tint special_ret;\n+\n+\t\tspecial_ret = commit_ref_update(refs, data->lock, &data->orig_oid,\n+\t\t\t\t\t\ttransaction->logmsg, 0, err);\n+\t\tif (special_ret) {\n+\t\t\terror(\"unable to write current sha1 into %s: %s\",\n+\t\t\t      transaction->new_refname, err->buf);\n+\t\t\tdata->lock = NULL;\n+\t\t\tfiles_transaction_abort(ref_store, transaction, err);\n+\t\t\treturn special_ret;\n+\t\t} else if (data->destination_log_backed_up) {\n+\t\t\tstruct strbuf path = STRBUF_INIT;\n+\n+\t\t\tfiles_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);\n+\t\t\tif (unlink(path.buf) < 0 && errno != ENOENT)\n+\t\t\t\twarning_errno(\"unable to remove '%s'\", path.buf);\n+\t\t\tstrbuf_release(&path);\n+\t\t}\n+\t\tfree(data->destination_target);\n+\t\tfree(data);\n+\t\ttransaction->backend_data = NULL;\n+\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\t\treturn special_ret;\n+\t}\n \n \tif (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n \t\treturn files_transaction_finish_initial(refs, transaction, err);\n@@ -3476,11 +3607,98 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \n static int files_transaction_abort(struct ref_store *ref_store,\n \t\t\t\t   struct ref_transaction *transaction,\n-\t\t\t\t   struct strbuf *err UNUSED)\n+\t\t\t\t   struct strbuf *err)\n {\n \tstruct files_ref_store *refs =\n \t\tfiles_downcast(ref_store, 0, \"ref_transaction_abort\");\n \n+\tif (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {\n+\t\tstruct files_copy_or_rename_transaction_data *data =\n+\t\t\ttransaction->backend_data;\n+\t\tstruct strbuf new_log = STRBUF_INIT;\n+\t\tstruct strbuf destination_log = STRBUF_INIT;\n+\t\tstruct strbuf temporary_log = STRBUF_INIT;\n+\t\tstruct ref_transaction *restore_transaction = NULL;\n+\t\tstruct ref_lock *lock;\n+\t\tint ret = 0;\n+\n+\t\tif (data->lock)\n+\t\t\tunlock_ref(data->lock);\n+\t\tif (transaction->type == REF_TRANSACTION_TYPE_RENAME) {\n+\t\t\tlock = lock_ref_oid_basic(refs, transaction->old_refname, err);\n+\t\t\tif (!lock ||\n+\t\t\t    write_ref_to_lockfile(refs, lock, &data->orig_oid, err) ||\n+\t\t\t    commit_ref_update(refs, lock, &data->orig_oid, NULL,\n+\t\t\t\t\t      REF_SKIP_CREATE_REFLOG, err))\n+\t\t\t\tret = -1;\n+\t\t}\n+\n+\t\tif (data->logmoved) {\n+\t\t\tfiles_reflog_path(refs, &new_log, transaction->new_refname);\n+\t\t\tif (transaction->type == REF_TRANSACTION_TYPE_RENAME) {\n+\t\t\t\tfiles_reflog_path(refs, &temporary_log, TMP_RENAMED_LOG);\n+\t\t\t\tif (rename(new_log.buf, temporary_log.buf) < 0) {\n+\t\t\t\t\tstrbuf_addf(err, \"unable to restore logfile %s: %s\",\n+\t\t\t\t\t\t    transaction->old_refname, strerror(errno));\n+\t\t\t\t\tret = -1;\n+\t\t\t\t} else {\n+\t\t\t\t\ttry_remove_empty_parents(refs,\n+\t\t\t\t\t\t\t transaction->new_refname,\n+\t\t\t\t\t\t\t REMOVE_EMPTY_PARENTS_REFLOG);\n+\t\t\t\t\tif (rename_tmp_log(refs,\n+\t\t\t\t\t\t\t   transaction->old_refname)) {\n+\t\t\t\t\t\tstrbuf_addf(err, \"unable to restore logfile %s: %s\",\n+\t\t\t\t\t\t\t    transaction->old_refname,\n+\t\t\t\t\t\t\t    strerror(errno));\n+\t\t\t\t\t\tret = -1;\n+\t\t\t\t\t}\n+\t\t\t\t}\n+\t\t\t} else if (unlink(new_log.buf) < 0 && errno != ENOENT) {\n+\t\t\t\tstrbuf_addf(err, \"unable to remove logfile %s: %s\",\n+\t\t\t\t\t    transaction->new_refname, strerror(errno));\n+\t\t\t\tret = -1;\n+\t\t\t}\n+\t\t}\n+\t\tif (data->destination_log_backed_up) {\n+\t\t\tfiles_reflog_path(refs, &destination_log,\n+\t\t\t\t\t   TMP_RENAMED_LOG_DESTINATION);\n+\t\t\tif (rename(destination_log.buf, new_log.buf) < 0) {\n+\t\t\t\tstrbuf_addf(err, \"unable to restore logfile %s: %s\",\n+\t\t\t\t\t    transaction->new_refname, strerror(errno));\n+\t\t\t\tret = -1;\n+\t\t\t}\n+\t\t}\n+\n+\t\tif (transaction->type == REF_TRANSACTION_TYPE_RENAME &&\n+\t\t    data->destination_exists) {\n+\t\t\trestore_transaction = ref_store_transaction_begin(\n+\t\t\t\t\t&refs->base, REF_TRANSACTION_FLAG_SKIP_HOOK, err);\n+\t\t\tif (!restore_transaction ||\n+\t\t\t    ref_transaction_update(restore_transaction,\n+\t\t\t\t\t\t   transaction->new_refname,\n+\t\t\t\t\t\t   data->destination_target ? NULL :\n+\t\t\t\t\t\t\t&data->destination_oid,\n+\t\t\t\t\t\t   NULL,\n+\t\t\t\t\t\t   data->destination_target,\n+\t\t\t\t\t\t   NULL,\n+\t\t\t\t\t\t   REF_NO_DEREF |\n+\t\t\t\t\t\t\tREF_SKIP_CREATE_REFLOG,\n+\t\t\t\t\t\t   NULL, err) ||\n+\t\t\t    ref_transaction_commit(restore_transaction, err))\n+\t\t\t\tret = -1;\n+\t\t\tref_transaction_free(restore_transaction);\n+\t\t}\n+\n+\t\tstrbuf_release(&destination_log);\n+\t\tstrbuf_release(&temporary_log);\n+\t\tstrbuf_release(&new_log);\n+\t\tfree(data->destination_target);\n+\t\tfree(data);\n+\t\ttransaction->backend_data = NULL;\n+\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\t\treturn ret;\n+\t}\n+\n \tfiles_transaction_cleanup(refs, transaction);\n \treturn 0;\n }\n@@ -4095,8 +4313,6 @@ struct ref_storage_be refs_be_files = {\n \n \t.optimize = files_optimize,\n \t.optimize_required = files_optimize_required,\n-\t.rename_ref = files_rename_ref,\n-\t.copy_ref = files_copy_ref,\n \n \t.iterator_begin = files_ref_iterator_begin,\n \t.read_raw_ref = files_read_raw_ref,\ndiff --git a/refs/packed-backend.c b/refs/packed-backend.c\nindex a73fc6aca..364a91291 100644\n--- a/refs/packed-backend.c\n+++ b/refs/packed-backend.c\n@@ -2164,8 +2164,6 @@ struct ref_storage_be refs_be_packed = {\n \t.optimize = packed_optimize,\n \t.optimize_required = packed_optimize_required,\n \n-\t.rename_ref = NULL,\n-\t.copy_ref = NULL,\n \n \t.iterator_begin = packed_ref_iterator_begin,\n \t.read_raw_ref = packed_read_raw_ref,\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex c3ac7b556..406b54b65 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -187,6 +187,13 @@ struct ref_update *ref_transaction_add_update(\n \t\tconst char *committer_info,\n \t\tconst char *msg);\n \n+int refs_delete_ref_with_transaction_flags(struct ref_store *refs,\n+\t\t\t\t\t   const char *msg,\n+\t\t\t\t\t   const char *refname,\n+\t\t\t\t\t   const struct object_id *old_oid,\n+\t\t\t\t\t   unsigned int flags,\n+\t\t\t\t\t   unsigned int transaction_flags);\n+\n /*\n  * Transaction states.\n  *\n@@ -212,6 +219,12 @@ enum ref_transaction_state {\n \tREF_TRANSACTION_CLOSED   = 2\n };\n \n+enum ref_transaction_type {\n+\tREF_TRANSACTION_TYPE_NORMAL = 0,\n+\tREF_TRANSACTION_TYPE_RENAME,\n+\tREF_TRANSACTION_TYPE_COPY,\n+};\n+\n /*\n  * Data structure to hold indices of updates which were rejected, for batched\n  * reference updates. While the updates themselves hold the rejection error,\n@@ -240,6 +253,21 @@ struct ref_transaction {\n \tvoid *backend_data;\n \tunsigned int flags;\n \tuint64_t max_index;\n+\n+\t/*\n+\t * Rename and copy operations need backend-specific reflog handling.\n+\t * Their logical updates still live in `updates`, so hooks see the\n+\t * operation like any other reference transaction. The fields below\n+\t * retain the state that backends verify after taking their locks.\n+\t */\n+\tenum ref_transaction_type type;\n+\tchar *old_refname;\n+\tchar *new_refname;\n+\tchar *logmsg;\n+\tstruct object_id source_oid;\n+\tstruct object_id destination_oid;\n+\tchar *destination_target;\n+\tunsigned int destination_exists:1;\n };\n \n /*\n@@ -451,13 +479,6 @@ typedef int optimize_required_fn(struct ref_store *ref_store,\n \t\t\t\t struct refs_optimize_opts *opts,\n \t\t\t\t bool *required);\n \n-typedef int rename_ref_fn(struct ref_store *ref_store,\n-\t\t\t  const char *oldref, const char *newref,\n-\t\t\t  const char *logmsg);\n-typedef int copy_ref_fn(struct ref_store *ref_store,\n-\t\t\t  const char *oldref, const char *newref,\n-\t\t\t  const char *logmsg);\n-\n /*\n  * Iterate over the references in `ref_store` whose names start with\n  * `prefix`. `prefix` is matched as a literal string, without regard\n@@ -577,9 +598,6 @@ struct ref_storage_be {\n \n \toptimize_fn *optimize;\n \toptimize_required_fn *optimize_required;\n-\trename_ref_fn *rename_ref;\n-\tcopy_ref_fn *copy_ref;\n-\n \tref_iterator_begin_fn *iterator_begin;\n \tread_raw_ref_fn *read_raw_ref;\n \ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 10db03991..cf8a16af5 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -953,6 +953,14 @@ struct reftable_transaction_data {\n \tsize_t args_nr, args_alloc;\n };\n \n+struct reftable_copy_or_rename_transaction_data {\n+\tstruct reftable_addition *addition;\n+};\n+\n+static int reftable_be_copy_or_rename_prepare(struct ref_store *ref_store,\n+\t\t\t\t\t       struct ref_transaction *transaction,\n+\t\t\t\t\t       struct strbuf *err);\n+\n static void free_transaction_data(struct reftable_transaction_data *tx_data)\n {\n \tif (!tx_data)\n@@ -1326,6 +1334,10 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \tsize_t i;\n \tint ret;\n \n+\tif (transaction->type != REF_TRANSACTION_TYPE_NORMAL)\n+\t\treturn reftable_be_copy_or_rename_prepare(ref_store, transaction,\n+\t\t\t\t\t\t\t   err);\n+\n \tret = refs->err;\n \tif (ret < 0)\n \t\tgoto done;\n@@ -1419,7 +1431,20 @@ static int reftable_be_transaction_abort(struct ref_store *ref_store UNUSED,\n \t\t\t\t\t struct ref_transaction *transaction,\n \t\t\t\t\t struct strbuf *err UNUSED)\n {\n-\tstruct reftable_transaction_data *tx_data = transaction->backend_data;\n+\tstruct reftable_transaction_data *tx_data;\n+\n+\tif (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {\n+\t\tstruct reftable_copy_or_rename_transaction_data *data =\n+\t\t\ttransaction->backend_data;\n+\n+\t\treftable_addition_destroy(data->addition);\n+\t\tfree(data);\n+\t\ttransaction->backend_data = NULL;\n+\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\t\treturn 0;\n+\t}\n+\n+\ttx_data = transaction->backend_data;\n \tfree_transaction_data(tx_data);\n \ttransaction->state = REF_TRANSACTION_CLOSED;\n \treturn 0;\n@@ -1667,9 +1692,28 @@ static int reftable_be_transaction_finish(struct ref_store *ref_store UNUSED,\n \t\t\t\t\t  struct ref_transaction *transaction,\n \t\t\t\t\t  struct strbuf *err)\n {\n-\tstruct reftable_transaction_data *tx_data = transaction->backend_data;\n+\tstruct reftable_transaction_data *tx_data;\n \tint ret = 0;\n \n+\tif (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {\n+\t\tstruct reftable_copy_or_rename_transaction_data *data =\n+\t\t\ttransaction->backend_data;\n+\t\tint special_ret = reftable_addition_commit(data->addition);\n+\n+\t\treftable_addition_destroy(data->addition);\n+\t\tfree(data);\n+\t\ttransaction->backend_data = NULL;\n+\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\t\tif (special_ret < 0) {\n+\t\t\tstrbuf_addf(err, _(\"reftable: transaction failure: %s\"),\n+\t\t\t\t    reftable_error_str(special_ret));\n+\t\t\treturn -1;\n+\t\t}\n+\t\treturn 0;\n+\t}\n+\n+\ttx_data = transaction->backend_data;\n+\n \tfor (size_t i = 0; i < tx_data->args_nr; i++) {\n \t\ttx_data->args[i].max_index = transaction->max_index;\n \n@@ -1764,17 +1808,20 @@ struct write_create_symref_arg {\n struct write_copy_arg {\n \tstruct reftable_ref_store *refs;\n \tstruct reftable_backend *be;\n+\tstruct strbuf *err;\n \tconst char *oldname;\n \tconst char *newname;\n \tconst char *logmsg;\n \tint delete_old;\n+\tstruct ref_transaction *transaction;\n };\n \n static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n {\n \tstruct write_copy_arg *arg = cb_data;\n \tuint64_t deletion_ts, creation_ts;\n-\tstruct reftable_ref_record old_ref = {0}, refs[2] = {0};\n+\tstruct reftable_ref_record old_ref = {0}, destination_ref = {0};\n+\tstruct reftable_ref_record refs[2] = {0};\n \tstruct reftable_log_record old_log = {0}, *logs = NULL;\n \tstruct reftable_iterator it = {0};\n \tstruct string_list skip = STRING_LIST_INIT_NODUP;\n@@ -1789,14 +1836,75 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n \t\tBUG(\"failed splitting committer info\");\n \n \tif (reftable_stack_read_ref(arg->be->stack, arg->oldname, &old_ref)) {\n-\t\tret = error(_(\"refname %s not found\"), arg->oldname);\n+\t\tstrbuf_addf(arg->err, _(\"refname %s not found\"), arg->oldname);\n+\t\tret = -1;\n \t\tgoto done;\n \t}\n \tif (old_ref.value_type == REFTABLE_REF_SYMREF) {\n-\t\tret = error(_(\"refname %s is a symbolic ref, copying it is not supported\"),\n+\t\tstrbuf_addf(arg->err,\n+\t\t\t    _(\"refname %s is a symbolic ref, copying it is not supported\"),\n \t\t\t    arg->oldname);\n+\t\tret = -1;\n \t\tgoto done;\n \t}\n+\tif (arg->transaction) {\n+\t\tstruct object_id oid;\n+\n+\t\tif (old_ref.value_type == REFTABLE_REF_VAL2)\n+\t\t\toidread(&oid, old_ref.value.val2.value,\n+\t\t\t\targ->refs->base.repo->hash_algo);\n+\t\telse\n+\t\t\toidread(&oid, old_ref.value.val1,\n+\t\t\t\targ->refs->base.repo->hash_algo);\n+\t\tif (!oideq(&oid, &arg->transaction->source_oid)) {\n+\t\t\tstrbuf_addf(arg->err,\n+\t\t\t\t    _(\"refname %s is at %s but expected %s\"),\n+\t\t\t\t    arg->oldname, oid_to_hex(&oid),\n+\t\t\t\t    oid_to_hex(&arg->transaction->source_oid));\n+\t\t\tret = -1;\n+\t\t\tgoto done;\n+\t\t}\n+\n+\t\tret = reftable_stack_read_ref(arg->be->stack, arg->newname,\n+\t\t\t\t\t      &destination_ref);\n+\t\tif (ret < 0)\n+\t\t\tgoto done;\n+\t\tif (arg->transaction->destination_exists != !ret) {\n+\t\t\tstrbuf_addf(arg->err,\n+\t\t\t\t    _(\"refname %s changed while renaming\"),\n+\t\t\t\t    arg->newname);\n+\t\t\tret = -1;\n+\t\t\tgoto done;\n+\t\t}\n+\t\tif (!ret) {\n+\t\t\tif (destination_ref.value_type == REFTABLE_REF_SYMREF) {\n+\t\t\t\tif (!arg->transaction->destination_target ||\n+\t\t\t\t    strcmp(destination_ref.value.symref,\n+\t\t\t\t\t   arg->transaction->destination_target)) {\n+\t\t\t\t\tstrbuf_addf(arg->err,\n+\t\t\t\t\t\t    _(\"refname %s changed while renaming\"),\n+\t\t\t\t\t\t    arg->newname);\n+\t\t\t\t\tret = -1;\n+\t\t\t\t\tgoto done;\n+\t\t\t\t}\n+\t\t\t} else {\n+\t\t\t\tif (destination_ref.value_type == REFTABLE_REF_VAL2)\n+\t\t\t\t\toidread(&oid, destination_ref.value.val2.value,\n+\t\t\t\t\t\targ->refs->base.repo->hash_algo);\n+\t\t\t\telse\n+\t\t\t\t\toidread(&oid, destination_ref.value.val1,\n+\t\t\t\t\t\targ->refs->base.repo->hash_algo);\n+\t\t\t\tif (arg->transaction->destination_target ||\n+\t\t\t\t    !oideq(&oid, &arg->transaction->destination_oid)) {\n+\t\t\t\t\tstrbuf_addf(arg->err,\n+\t\t\t\t\t\t    _(\"refname %s changed while renaming\"),\n+\t\t\t\t\t\t    arg->newname);\n+\t\t\t\t\tret = -1;\n+\t\t\t\t\tgoto done;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t}\n+\t}\n \n \t/*\n \t * There's nothing to do in case the old and new name are the same, so\n@@ -1815,7 +1923,7 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n \tret = refs_verify_refname_available(&arg->refs->base, arg->newname,\n \t\t\t\t\t    NULL, &skip, 0, &errbuf);\n \tif (ret < 0) {\n-\t\terror(\"%s\", errbuf.buf);\n+\t\tstrbuf_addbuf(arg->err, &errbuf);\n \t\tgoto done;\n \t}\n \n@@ -1980,68 +2088,60 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n \tfor (i = 0; i < ARRAY_SIZE(refs); i++)\n \t\treftable_ref_record_release(&refs[i]);\n \treftable_ref_record_release(&old_ref);\n+\treftable_ref_record_release(&destination_ref);\n \treftable_log_record_release(&old_log);\n \treturn ret;\n }\n \n-static int reftable_be_rename_ref(struct ref_store *ref_store,\n-\t\t\t\t  const char *oldrefname,\n-\t\t\t\t  const char *newrefname,\n-\t\t\t\t  const char *logmsg)\n+static int reftable_be_copy_or_rename_prepare(struct ref_store *ref_store,\n+\t\t\t\t\t       struct ref_transaction *transaction,\n+\t\t\t\t\t       struct strbuf *err)\n {\n \tstruct reftable_ref_store *refs =\n-\t\treftable_be_downcast(ref_store, REF_STORE_WRITE, \"rename_ref\");\n+\t\treftable_be_downcast(ref_store, REF_STORE_WRITE,\n+\t\t\t\t     \"ref_transaction_prepare\");\n+\tstruct reftable_copy_or_rename_transaction_data *data = NULL;\n \tstruct write_copy_arg arg = {\n \t\t.refs = refs,\n-\t\t.oldname = oldrefname,\n-\t\t.newname = newrefname,\n-\t\t.logmsg = logmsg,\n-\t\t.delete_old = 1,\n+\t\t.err = err,\n+\t\t.oldname = transaction->old_refname,\n+\t\t.newname = transaction->new_refname,\n+\t\t.logmsg = transaction->logmsg,\n+\t\t.delete_old = transaction->type == REF_TRANSACTION_TYPE_RENAME,\n+\t\t.transaction = transaction,\n \t};\n \tint ret;\n \n+\tCALLOC_ARRAY(data, 1);\n \tret = refs->err;\n \tif (ret < 0)\n \t\tgoto done;\n-\n-\tret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);\n+\tret = backend_for(&arg.be, refs, transaction->new_refname,\n+\t\t\t  &arg.newname, 1);\n \tif (ret)\n \t\tgoto done;\n-\tret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,\n-\t\t\t\t &reftable_be_write_options(refs)->opts);\n-\n-done:\n-\tassert(ret != REFTABLE_API_ERROR);\n-\treturn ret;\n-}\n-\n-static int reftable_be_copy_ref(struct ref_store *ref_store,\n-\t\t\t\tconst char *oldrefname,\n-\t\t\t\tconst char *newrefname,\n-\t\t\t\tconst char *logmsg)\n-{\n-\tstruct reftable_ref_store *refs =\n-\t\treftable_be_downcast(ref_store, REF_STORE_WRITE, \"copy_ref\");\n-\tstruct write_copy_arg arg = {\n-\t\t.refs = refs,\n-\t\t.oldname = oldrefname,\n-\t\t.newname = newrefname,\n-\t\t.logmsg = logmsg,\n-\t};\n-\tint ret;\n-\n-\tret = refs->err;\n-\tif (ret < 0)\n+\tret = reftable_stack_addition_new(&data->addition, arg.be->stack,\n+\t\t\t\t\t  &reftable_be_write_options(refs)->opts);\n+\tif (ret)\n \t\tgoto done;\n-\n-\tret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);\n+\tret = reftable_addition_add(data->addition, &write_copy_table, &arg);\n \tif (ret)\n \t\tgoto done;\n-\tret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,\n-\t\t\t\t &reftable_be_write_options(refs)->opts);\n+\n+\ttransaction->backend_data = data;\n+\ttransaction->state = REF_TRANSACTION_PREPARED;\n+\treturn 0;\n \n done:\n \tassert(ret != REFTABLE_API_ERROR);\n+\tif (data) {\n+\t\treftable_addition_destroy(data->addition);\n+\t\tfree(data);\n+\t}\n+\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\tif (ret && !err->len)\n+\t\tstrbuf_addf(err, _(\"reftable: transaction prepare: %s\"),\n+\t\t\t    reftable_error_str(ret));\n \treturn ret;\n }\n \n@@ -2872,8 +2972,6 @@ struct ref_storage_be refs_be_reftable = {\n \t.optimize = reftable_be_optimize,\n \t.optimize_required = reftable_be_optimize_required,\n \n-\t.rename_ref = reftable_be_rename_ref,\n-\t.copy_ref = reftable_be_copy_ref,\n \n \t.iterator_begin = reftable_be_iterator_begin,\n \t.read_raw_ref = reftable_be_read_raw_ref,\ndiff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh\nindex 4fe9d9b23..116b2ff07 100755\n--- a/t/t1416-ref-transaction-hooks.sh\n+++ b/t/t1416-ref-transaction-hooks.sh\n@@ -93,6 +93,148 @@ test_expect_success 'hook gets all queued updates in committed state' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'hook gets both updates when renaming a branch' '\n+\ttest_when_finished \"rm -f actual\" &&\n+\tgit branch old PRE &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\techo \"$1\" >>actual &&\n+\t\tcat >>actual\n+\tEOF\n+\tcat >expect <<-EOF &&\n+\tpreparing\n+\t$PRE_OID $ZERO_OID refs/heads/old\n+\t$ZERO_OID $PRE_OID refs/heads/new\n+\tprepared\n+\t$PRE_OID $ZERO_OID refs/heads/old\n+\t$ZERO_OID $PRE_OID refs/heads/new\n+\tcommitted\n+\t$PRE_OID $ZERO_OID refs/heads/old\n+\t$ZERO_OID $PRE_OID refs/heads/new\n+\tEOF\n+\tgit branch -m old new &&\n+\ttest_cmp expect actual &&\n+\ttest_must_fail git rev-parse --verify refs/heads/old &&\n+\ttest_cmp_rev PRE refs/heads/new\n+'\n+\n+test_expect_success 'hook gets destination update when copying a branch' '\n+\ttest_when_finished \"rm -f actual\" &&\n+\tgit branch copy-source PRE &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\techo \"$1\" >>actual &&\n+\t\tcat >>actual\n+\tEOF\n+\tcat >expect <<-EOF &&\n+\tpreparing\n+\t$ZERO_OID $PRE_OID refs/heads/copy-destination\n+\tprepared\n+\t$ZERO_OID $PRE_OID refs/heads/copy-destination\n+\tcommitted\n+\t$ZERO_OID $PRE_OID refs/heads/copy-destination\n+\tEOF\n+\tgit branch -c copy-source copy-destination &&\n+\ttest_cmp expect actual &&\n+\ttest_cmp_rev PRE refs/heads/copy-source &&\n+\ttest_cmp_rev PRE refs/heads/copy-destination\n+'\n+\n+test_expect_success 'hook gets overwritten values for forced rename and copy' '\n+\tgit branch force-old PRE &&\n+\tgit branch force-new POST &&\n+\tgit branch force-copy-source PRE &&\n+\tgit branch force-copy-destination POST &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\tif test \"$1\" = committed\n+\t\tthen\n+\t\t\tcat >>actual\n+\t\tfi\n+\tEOF\n+\tgit branch -M force-old force-new &&\n+\tgit branch -C force-copy-source force-copy-destination &&\n+\tcat >expect <<-EOF &&\n+\t$PRE_OID $ZERO_OID refs/heads/force-old\n+\t$POST_OID $PRE_OID refs/heads/force-new\n+\t$POST_OID $PRE_OID refs/heads/force-copy-destination\n+\tEOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'hook can abort a branch rename after preparation' '\n+\tgit branch abort-old PRE &&\n+\tgit branch abort-new POST &&\n+\tgit reflog show --format=%gs abort-old >old-log &&\n+\tgit reflog show --format=%gs abort-new >new-log &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\ttest_must_fail git branch -M abort-old abort-new &&\n+\ttest_cmp_rev PRE refs/heads/abort-old &&\n+\ttest_cmp_rev POST refs/heads/abort-new &&\n+\tgit reflog show --format=%gs abort-old >old-log-after &&\n+\tgit reflog show --format=%gs abort-new >new-log-after &&\n+\ttest_cmp old-log old-log-after &&\n+\ttest_cmp new-log new-log-after\n+'\n+\n+test_expect_success 'hook can abort a D/F branch rename after preparation' '\n+\tgit branch df-old PRE &&\n+\tgit reflog show --format=%gs df-old >df-log &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\ttest_must_fail git branch -m df-old df-old/child &&\n+\ttest_cmp_rev PRE refs/heads/df-old &&\n+\ttest_must_fail git rev-parse --verify refs/heads/df-old/child &&\n+\tgit reflog show --format=%gs df-old >df-log-after &&\n+\ttest_cmp df-log df-log-after\n+'\n+\n+test_expect_success 'hook can abort a reverse D/F rename after preparation' '\n+\tgit branch reverse/old PRE &&\n+\tgit reflog show --format=%gs reverse/old >reverse-log &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\ttest_must_fail git branch -m reverse/old reverse &&\n+\ttest_cmp_rev PRE refs/heads/reverse/old &&\n+\ttest_must_fail git rev-parse --verify refs/heads/reverse &&\n+\tgit reflog show --format=%gs reverse/old >reverse-log-after &&\n+\ttest_cmp reverse-log reverse-log-after\n+'\n+\n+test_expect_success 'hook can abort a forced branch copy after preparation' '\n+\tgit branch copy-abort-old PRE &&\n+\tgit branch copy-abort-new POST &&\n+\tgit reflog show --format=%gs copy-abort-old >copy-old-log &&\n+\tgit reflog show --format=%gs copy-abort-new >copy-new-log &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\ttest_must_fail git branch -C copy-abort-old copy-abort-new &&\n+\ttest_cmp_rev PRE refs/heads/copy-abort-old &&\n+\ttest_cmp_rev POST refs/heads/copy-abort-new &&\n+\tgit reflog show --format=%gs copy-abort-old >copy-old-log-after &&\n+\tgit reflog show --format=%gs copy-abort-new >copy-new-log-after &&\n+\ttest_cmp copy-old-log copy-old-log-after &&\n+\ttest_cmp copy-new-log copy-new-log-after\n+'\n+\n+test_expect_success 'branch rename detects an update during preparing hook' '\n+\tgit branch race-old PRE &&\n+\tgit branch race-new POST &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\tmarker=$(git rev-parse --git-path rename-race-once)\n+\t\tif test \"$1\" = preparing && test ! -e \"$marker\"\n+\t\tthen\n+\t\t\t>\"$marker\" &&\n+\t\t\tgit update-ref refs/heads/race-old POST\n+\t\tfi\n+\tEOF\n+\ttest_must_fail git branch -M race-old race-new &&\n+\ttest_cmp_rev POST refs/heads/race-old &&\n+\ttest_cmp_rev POST refs/heads/race-new\n+'\n+\n test_expect_success 'hook gets all queued updates in aborted state' '\n \ttest_when_finished \"rm actual\" &&\n \tgit reset --hard PRE &&\n\nbase-commit: d38352cd43ab9745686d697872408bc3249a153f\n-- \n2.39.3 (Apple Git-146)\n\n"},{"id":"552943","messageId":"xmqqjyoemqvu.fsf@gitster.g","threadId":"66350","inReplyTo":"20260920165037.88524-1-maciej.ciemborowicz@gmail.com","subject":"Re: [PATCH] refs: run copy and rename through transactions","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-21T17:54:45Z","receivedAt":"2026-09-21T17:54:47Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com> writes:\n\n> Reference copy and rename operations currently bypass the transaction API.\n> Consequently, the reference-transaction hook sees only the source deletion\n> with the files backend and no useful update with the reftable backend.\n>\n> Represent both operations as reference transactions containing their\n> logical updates. A rename is a deletion of the old reference and creation\n> of the new reference in the same transaction. Retain backend-specific\n> reflog handling: the files backend stages its existing rename procedure\n> across prepare, finish and abort, while reftable stages an addition while\n> holding the stack lock. Suppress hooks for the files backend's nested\n> deletion transactions so that callers observe one logical transaction.\n>\n> Record and verify the source and destination values after taking backend\n> locks. This rejects concurrent changes instead of applying a rename or copy\n> that differs from the payload shown to the preparing hook. Preserve D/F\n> renames and restore overwritten references and reflogs when a prepared hook\n> rejects the operation.\n>\n> Add coverage for rename, copy, forced updates, both directions of D/F\n> conflicts, concurrent updates and prepared-hook rollback.\n>\n> Helped-by: Karthik Nayak <karthik.188@gmail.com>\n> Signed-off-by: Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com>\n> ---\n\nDrop unnecessary \"currently\" to the first sentence, and add \"test\"\nto the laste sentence somewhere, and this would be perfect.\n\nVery pleasing to see an exceptionally well-written proposed commit\nlog message by a new contributor.\n\n>  refs.c                           | 137 ++++++++++++---\n>  refs.h                           |   3 +\n>  refs/debug.c                     |  25 ---\n>  refs/files-backend.c             | 276 +++++++++++++++++++++++++++----\n>  refs/packed-backend.c            |   2 -\n>  refs/refs-internal.h             |  38 +++--\n>  refs/reftable-backend.c          | 194 ++++++++++++++++------\n>  t/t1416-ref-transaction-hooks.sh | 142 ++++++++++++++++\n>  8 files changed, 679 insertions(+), 138 deletions(-)\n>\n> diff --git a/refs.c b/refs.c\n> index 92d5df5b7..22c000f7f 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -1004,15 +1004,17 @@ long get_files_ref_lock_timeout_ms(struct repository *repo)\n>  \treturn timeout_ms;\n>  }\n>  \n> -int refs_delete_ref(struct ref_store *refs, const char *msg,\n> -\t\t    const char *refname,\n> -\t\t    const struct object_id *old_oid,\n> -\t\t    unsigned int flags)\n> +int refs_delete_ref_with_transaction_flags(struct ref_store *refs,\n> +\t\t\t\t\t   const char *msg,\n> +\t\t\t\t\t   const char *refname,\n> +\t\t\t\t\t   const struct object_id *old_oid,\n> +\t\t\t\t\t   unsigned int flags,\n> +\t\t\t\t\t   unsigned int transaction_flags)\n>  {\n>  \tstruct ref_transaction *transaction;\n>  \tstruct strbuf err = STRBUF_INIT;\n>  \n> -\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n> +\ttransaction = ref_store_transaction_begin(refs, transaction_flags, &err);\n>  \tif (!transaction ||\n>  \t    ref_transaction_delete(transaction, refname, old_oid,\n>  \t\t\t\t   NULL, flags, msg, &err) ||\n> @@ -1027,6 +1029,15 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,\n>  \treturn 0;\n>  }\n>  \n> +int refs_delete_ref(struct ref_store *refs, const char *msg,\n> +\t\t    const char *refname,\n> +\t\t    const struct object_id *old_oid,\n> +\t\t    unsigned int flags)\n> +{\n> +\treturn refs_delete_ref_with_transaction_flags(refs, msg, refname,\n> +\t\t\t\t\t\t      old_oid, flags, 0);\n> +}\n> +\n>  static void copy_reflog_msg(struct strbuf *sb, const char *msg)\n>  {\n>  \tchar c;\n> @@ -1270,6 +1281,10 @@ void ref_transaction_free(struct ref_transaction *transaction)\n>  \n>  \tstring_list_clear(&transaction->refnames, 0);\n>  \tfree(transaction->updates);\n> +\tfree(transaction->old_refname);\n> +\tfree(transaction->new_refname);\n> +\tfree(transaction->logmsg);\n> +\tfree(transaction->destination_target);\n>  \tfree(transaction);\n>  }\n>  \n> @@ -2710,7 +2725,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,\n>  \t\treturn REF_TRANSACTION_ERROR_GENERIC;\n>  \n>  \t/* Preparing checks before locking references */\n> -\tret = run_transaction_hook(transaction, \"preparing\");\n> +\tret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :\n> +\t\trun_transaction_hook(transaction, \"preparing\");\n>  \tif (ret) {\n>  \t\tref_transaction_abort(transaction, err);\n>  \t\tdie(_(abort_by_ref_transaction_hook), \"preparing\");\n> @@ -2720,7 +2736,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,\n>  \tif (ret)\n>  \t\treturn ret;\n>  \n> -\tret = run_transaction_hook(transaction, \"prepared\");\n> +\tret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :\n> +\t\trun_transaction_hook(transaction, \"prepared\");\n>  \tif (ret) {\n>  \t\tref_transaction_abort(transaction, err);\n>  \t\tdie(_(abort_by_ref_transaction_hook), \"prepared\");\n> @@ -2750,7 +2767,8 @@ int ref_transaction_abort(struct ref_transaction *transaction,\n>  \t\tbreak;\n>  \t}\n>  \n> -\trun_transaction_hook(transaction, \"aborted\");\n> +\tif (!(transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK))\n> +\t\trun_transaction_hook(transaction, \"aborted\");\n>  \n>  \tref_transaction_free(transaction);\n>  \treturn ret;\n> @@ -2781,7 +2799,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n>  \t}\n>  \n>  \tret = refs->be->transaction_finish(refs, transaction, err);\n> -\tif (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL))\n> +\tif (!ret && !(transaction->flags & (REF_TRANSACTION_FLAG_INITIAL |\n> +\t\t\t\t\t REF_TRANSACTION_FLAG_SKIP_HOOK)))\n>  \t\trun_transaction_hook(transaction, \"committed\");\n>  \treturn ret;\n>  }\n> @@ -3123,28 +3142,100 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n>  \treturn ret;\n>  }\n>  \n> -int refs_rename_ref(struct ref_store *refs, const char *oldref,\n> -\t\t    const char *newref, const char *logmsg)\n\nIt is annoying that we have to give random callers an unrestricted\nway to skip calling hooks.  I suspect it may come from \"this\nfunction should call hook when invoked as the top-level operation,\nbut when it is used as a subroutine for a different top-level\noperation, we want to skip hooks\" kind of reasoning, but is this\nsomething we can avoid by rearranging the call chain?\n\n> +static int refs_copy_or_rename_ref(struct ref_store *refs, const char *oldref,\n> +\t\t\t\t   const char *newref, const char *logmsg,\n> +\t\t\t\t   int copy)\n\nWill this function ever gain a third mode of operation other than\ncopy or rename?  If not, perhaps \"bool copy\"?\n\n>  {\n> -\tchar *msg;\n> -\tint retval;\n> +\tstruct ref_transaction *transaction = NULL;\n> +\tstruct object_id old_oid, new_oid;\n> +\tstruct strbuf new_target = STRBUF_INIT;\n> +\tstruct strbuf err = STRBUF_INIT;\n> +\tchar *msg = normalize_reflog_message(logmsg);\n> +\tint old_flags, new_flags = 0, new_exists = 0, ret = 1;\n>  \n> -\tmsg = normalize_reflog_message(logmsg);\n> -\tretval = refs->be->rename_ref(refs, oldref, newref, msg);\n> +\tif (!strcmp(oldref, newref)) {\n> +\t\tret = 0;\n> +\t\tgoto out;\n> +\t}\n> +\n> +\tif (!refs_resolve_ref_unsafe(refs, oldref,\n> +\t\t\t\t     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n> +\t\t\t\t     &old_oid, &old_flags)) {\n> +\t\terror(\"refname %s not found\", oldref);\n> +\t\tgoto out;\n> +\t}\n> +\tif (old_flags & REF_ISSYMREF) {\n> +\t\terror(\"refname %s is a symbolic ref, %s it is not supported\",\n> +\t\t      oldref, copy ? \"copying\" : \"renaming\");\n> +\t\tgoto out;\n> +\t}\n> +\n> +\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n> +\tif (!transaction)\n> +\t\tgoto error;\n> +\ttransaction->type = copy ? REF_TRANSACTION_TYPE_COPY :\n> +\t\tREF_TRANSACTION_TYPE_RENAME;\n> +\ttransaction->old_refname = xstrdup(oldref);\n> +\ttransaction->new_refname = xstrdup(newref);\n> +\ttransaction->logmsg = xstrdup(msg);\n> +\toidcpy(&transaction->source_oid, &old_oid);\n> +\n> +\tif (!copy && ref_transaction_delete(transaction, oldref, &old_oid, NULL,\n> +\t\t\t\t\t    REF_NO_DEREF, msg, &err))\n> +\t\tgoto error;\n> +\n> +\tif (refs_resolve_ref_unsafe(refs, newref,\n> +\t\t\t\t    RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n> +\t\t\t\t    &new_oid, &new_flags)) {\n> +\t\tnew_exists = 1;\n> +\t\tif ((new_flags & REF_ISSYMREF) &&\n> +\t\t    refs_read_symbolic_ref(refs, newref, &new_target) < 0) {\n> +\t\t\tstrbuf_addf(&err, \"unable to read symbolic ref %s\", newref);\n> +\t\t\tgoto error;\n> +\t\t}\n> +\t} else {\n> +\t\toidclr(&new_oid, refs->repo->hash_algo);\n> +\t}\n> +\ttransaction->destination_exists = new_exists;\n> +\tif (new_flags & REF_ISSYMREF)\n> +\t\ttransaction->destination_target = xstrdup(new_target.buf);\n> +\telse if (transaction->destination_exists)\n> +\t\toidcpy(&transaction->destination_oid, &new_oid);\n> +\n> +\tif (ref_transaction_update(transaction, newref, &old_oid,\n> +\t\t\t\t   (new_flags & REF_ISSYMREF) ? NULL : &new_oid,\n> +\t\t\t\t   NULL,\n> +\t\t\t\t   (new_flags & REF_ISSYMREF) ? new_target.buf : NULL,\n> +\t\t\t\t   REF_NO_DEREF | REF_SKIP_CREATE_REFLOG,\n> +\t\t\t\t   NULL, &err))\n> +\t\tgoto error;\n> +\n> +\tif (ref_transaction_commit(transaction, &err))\n> +\t\tgoto error;\n> +\n> +\tret = 0;\n> +\tgoto out;\n> +\n> +error:\n> +\terror(\"%s\", err.buf);\n> +out:\n> +\tref_transaction_free(transaction);\n> +\tstrbuf_release(&new_target);\n> +\tstrbuf_release(&err);\n>  \tfree(msg);\n> -\treturn retval;\n> +\treturn ret;\n>  }\n\nThat's quite a lot of new code.  I see ref_transaction_delete(),\nref_transaction_update() and others are already reused from existing\ncode paths, which is good.\n\n> +struct files_copy_or_rename_transaction_data {\n> +\tstruct ref_lock *lock;\n> +\tstruct object_id orig_oid;\n> +\tstruct object_id destination_oid;\n> +\tchar *destination_target;\n> +\tint logmoved;\n> +\tint destination_exists;\n> +\tint destination_log_backed_up;\n> +};\n\nGood to have a type that can be used to hold pieces of information\nspecific to the operation.  Can't we do without rename/copy specific\naddition to the generic ref_transaction struct by following the same\nprinciple?\n\nThe comment above the members does make it understandable, but ...\n\n> @@ -240,6 +253,21 @@ struct ref_transaction {\n>  \tvoid *backend_data;\n>  \tunsigned int flags;\n>  \tuint64_t max_index;\n> +\n> +\t/*\n> +\t * Rename and copy operations need backend-specific reflog handling.\n> +\t * Their logical updates still live in `updates`, so hooks see the\n> +\t * operation like any other reference transaction. The fields below\n> +\t * retain the state that backends verify after taking their locks.\n> +\t */\n> +\tenum ref_transaction_type type;\n> +\tchar *old_refname;\n> +\tchar *new_refname;\n> +\tchar *logmsg;\n> +\tstruct object_id source_oid;\n> +\tstruct object_id destination_oid;\n> +\tchar *destination_target;\n> +\tunsigned int destination_exists:1;\n>  };\n\n... is it the best we can do to contaminate a rather generic data\nstructure for such a details relevant only to one specific\noperation?\n\nThanks.\n"},{"id":"552960","messageId":"xmqqh5jijiar.fsf@gitster.g","threadId":"66350","inReplyTo":"xmqqjyoemqvu.fsf@gitster.g","subject":"Re: [PATCH] refs: run copy and rename through transactions","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-21T23:28:28Z","receivedAt":"2026-09-21T23:28:33Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n>> +struct files_copy_or_rename_transaction_data {\n>> +\tstruct ref_lock *lock;\n>> +\tstruct object_id orig_oid;\n>> +\tstruct object_id destination_oid;\n>> +\tchar *destination_target;\n>> +\tint logmoved;\n>> +\tint destination_exists;\n>> +\tint destination_log_backed_up;\n>> +};\n>\n> Good to have a type that can be used to hold pieces of information\n> specific to the operation.  Can't we do without rename/copy specific\n> addition to the generic ref_transaction struct by following the same\n> principle?\n>\n> The comment above the members does make it understandable, but ...\n>\n>> @@ -240,6 +253,21 @@ struct ref_transaction {\n>>  \tvoid *backend_data;\n>>  \tunsigned int flags;\n>>  \tuint64_t max_index;\n>> +\n>> +\t/*\n>> +\t * Rename and copy operations need backend-specific reflog handling.\n>> +\t * Their logical updates still live in `updates`, so hooks see the\n>> +\t * operation like any other reference transaction. The fields below\n>> +\t * retain the state that backends verify after taking their locks.\n>> +\t */\n>> +\tenum ref_transaction_type type;\n>> +\tchar *old_refname;\n>> +\tchar *new_refname;\n>> +\tchar *logmsg;\n>> +\tstruct object_id source_oid;\n>> +\tstruct object_id destination_oid;\n>> +\tchar *destination_target;\n>> +\tunsigned int destination_exists:1;\n>>  };\n>\n> ... is it the best we can do to contaminate a rather generic data\n> structure for such a details relevant only to one specific\n> operation?\n\nMore importantly, this structure suggests to me that you can have a\nsingle rename (or copy) from one source to one destination in a\nsingle transaction.  Is that correct or am I misunderstanding the\nway this data structure is used?  How would one rename A, B and C\nto X, Y and Z in a single transaction?  Or perhaps rename A to B\nand copy C to D in a single transaction?\n"},{"id":"552986","messageId":"CACQ=SRGicdcnyP6mFifdZgzCgoSgDPA=yrkr2gSPQ6_s7GJKsw@mail.gmail.com","threadId":"66350","inReplyTo":"xmqqh5jijiar.fsf@gitster.g","subject":"Re: [PATCH] refs: run copy and rename through transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-09-22T13:08:24Z","receivedAt":"2026-09-22T13:08:38Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"On Tue, Sep 22, 2026 at 1:28 AM Junio C Hamano <gitster@pobox.com> wrote:\n> More importantly, this structure suggests to me that you can have a\n> single rename (or copy) from one source to one destination in a\n> single transaction.  Is that correct or am I misunderstanding the\n> way this data structure is used?  How would one rename A, B and C\n> to X, Y and Z in a single transaction?  Or perhaps rename A to B\n> and copy C to D in a single transaction?\n\nGood point. The current design indeed makes copy/rename a property of\nthe whole transacton, so it does not compose with multiple such\noperations. I'll rework it so that  state belongs to individual\nupdates instead. Unfortunately, I'll have to put this aside for a few\ndays until I finish this first:\nhttps://lore.kernel.org/git/CACQ=SRHthWOLVXmY6wgknOPgpQ+oB1vV-Q0AL=mK9mXb2Xy9Nw@mail.gmail.com/T/#t\n\nThanks,\nMaciej Ciemborowicz\n"},{"id":"553046","messageId":"20260923124931.39424-1-maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"CAOLa=ZTN1TU2A1sgEhiw=ymMYr6Ge11cMEubSaeKqr4WNU=2EQ@mail.gmail.com","subject":"Re: [PATCH v4 0/3] refs: report old OIDs for batched deletions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-09-23T12:49:31Z","receivedAt":"2026-09-23T12:49:34Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"Hi Junio,\n\n> Can we avoid REF_TRANSACTION_FLAG_SKIP_HOOK by changing the call chain\n> instead of exposing a general mechanism for skipping hooks?\n\nI looked into that, but I do not think it removes the distinction\ncleanly. The packed-refs transaction is an internal, physical part of\none logical files-backend update: it needs the normal transaction\nmachinery, while the reference-transaction hook must observe only the\nouter logical update.\n\nRestructuring the call chain would either duplicate the\nprepare/finish/abort lifecycle or hide the same hook-suppression\ndecision in a less explicit helper. I do agree that this should not be\na public escape hatch, though. I will keep it internal to the refs\nimplementation and limit its use to these internal transactions.\n\n> Should the int copy parameter simply be bool copy, given that the\n> function has only two modes?\n\nYes, agreed. It is a two-state mode, so bool copy expresses the\ncontract more clearly. I will change it in the next reroll.\n\nThanks,\nMaciej\n"},{"id":"553060","messageId":"20260923133651.74120-1-maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"20260920165037.88524-1-maciej.ciemborowicz@gmail.com","subject":"[PATCH v2] refs: run copy and rename through transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-09-23T13:36:51Z","receivedAt":"2026-09-23T13:36:56Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"Reference copy and rename operations bypass the transaction API.\nConsequently, the reference-transaction hook sees only the source deletion\nwith the files backend and no useful update with the reftable backend.\n\nRepresent both operations as reference transactions containing their\nlogical updates. A rename is a deletion of the old reference and creation\nof the new reference in the same transaction. Attach operation-specific\nstate to the destination update instead of making copy or rename a property\nof the entire transaction.\n\nRetain backend-specific reflog handling: the files backend stages its\nexisting rename procedure across prepare, finish and abort, while reftable\nstages an addition while holding the stack lock. Suppress hooks for the\nfiles backend's nested deletion transactions so that callers observe one\nlogical transaction.\n\nRecord and verify the source and destination values after taking backend\nlocks. This rejects concurrent changes instead of applying a rename or copy\nthat differs from the payload shown to the preparing hook. Preserve D/F\nrenames and restore overwritten references and reflogs when a prepared hook\nrejects the operation.\n\nAdd tests covering rename, copy, forced updates, both directions of D/F\nconflicts, concurrent updates and prepared-hook rollback.\n\nHelped-by: Karthik Nayak <karthik.188@gmail.com>\nSigned-off-by: Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com>\n---\nApologies for the unrelated Subject header on my earlier reply. This\nreroll incorporates the points discussed there.\n\nChanges since v1:\n\n * Keep copy/rename state on the destination ref update instead of the\n   generic transaction, so the operation is no longer a transaction-wide\n   property.\n * Keep REF_TRANSACTION_FLAG_SKIP_HOOK private to the refs implementation.\n * Use bool for the two-state copy parameter.\n * Apply Junio's commit-message wording suggestions.\n\nRange-diff against v1:\n1:  de0a5a9f7 ! 1:  d852537d8 refs: run copy and rename through transactions\n    @@ Metadata\n      ## Commit message ##\n         refs: run copy and rename through transactions\n     \n    -    Reference copy and rename operations currently bypass the transaction API.\n    +    Reference copy and rename operations bypass the transaction API.\n         Consequently, the reference-transaction hook sees only the source deletion\n         with the files backend and no useful update with the reftable backend.\n     \n         Represent both operations as reference transactions containing their\n         logical updates. A rename is a deletion of the old reference and creation\n    -    of the new reference in the same transaction. Retain backend-specific\n    -    reflog handling: the files backend stages its existing rename procedure\n    -    across prepare, finish and abort, while reftable stages an addition while\n    -    holding the stack lock. Suppress hooks for the files backend's nested\n    -    deletion transactions so that callers observe one logical transaction.\n    +    of the new reference in the same transaction. Attach operation-specific\n    +    state to the destination update instead of making copy or rename a property\n    +    of the entire transaction.\n    +\n    +    Retain backend-specific reflog handling: the files backend stages its\n    +    existing rename procedure across prepare, finish and abort, while reftable\n    +    stages an addition while holding the stack lock. Suppress hooks for the\n    +    files backend's nested deletion transactions so that callers observe one\n    +    logical transaction.\n     \n         Record and verify the source and destination values after taking backend\n         locks. This rejects concurrent changes instead of applying a rename or copy\n    @@ Commit message\n         renames and restore overwritten references and reflogs when a prepared hook\n         rejects the operation.\n     \n    -    Add coverage for rename, copy, forced updates, both directions of D/F\n    +    Add tests covering rename, copy, forced updates, both directions of D/F\n         conflicts, concurrent updates and prepared-hook rollback.\n     \n         Helped-by: Karthik Nayak <karthik.188@gmail.com>\n    @@ refs.c: int refs_delete_ref(struct ref_store *refs, const char *msg,\n      {\n      \tchar c;\n     @@ refs.c: void ref_transaction_free(struct ref_transaction *transaction)\n    + \t}\n    + \n    + \tfor (i = 0; i < transaction->nr; i++) {\n    ++\t\tstruct ref_copy_or_rename_update *operation =\n    ++\t\t\ttransaction->updates[i]->copy_or_rename;\n    ++\n    + \t\tfree(transaction->updates[i]->msg);\n    + \t\tfree(transaction->updates[i]->committer_info);\n    + \t\tfree((char *)transaction->updates[i]->new_target);\n    + \t\tfree((char *)transaction->updates[i]->old_target);\n    + \t\tfree((char *)transaction->updates[i]->rejection_details);\n    ++\t\tif (operation) {\n    ++\t\t\tfree(operation->old_refname);\n    ++\t\t\tfree(operation->logmsg);\n    ++\t\t\tfree(operation->destination_target);\n    ++\t\t\tfree(operation);\n    ++\t\t}\n    + \t\tfree(transaction->updates[i]);\n    + \t}\n      \n    - \tstring_list_clear(&transaction->refnames, 0);\n    - \tfree(transaction->updates);\n    -+\tfree(transaction->old_refname);\n    -+\tfree(transaction->new_refname);\n    -+\tfree(transaction->logmsg);\n    -+\tfree(transaction->destination_target);\n    +@@ refs.c: void ref_transaction_free(struct ref_transaction *transaction)\n      \tfree(transaction);\n      }\n      \n    ++struct ref_update *ref_transaction_copy_or_rename_update(\n    ++\tstruct ref_transaction *transaction)\n    ++{\n    ++\tstruct ref_update *operation = NULL;\n    ++\tsize_t i;\n    ++\n    ++\tfor (i = 0; i < transaction->nr; i++) {\n    ++\t\tif (!transaction->updates[i]->copy_or_rename)\n    ++\t\t\tcontinue;\n    ++\t\tif (operation)\n    ++\t\t\tBUG(\"multiple copy or rename updates in one transaction\");\n    ++\t\toperation = transaction->updates[i];\n    ++\t}\n    ++\n    ++\treturn operation;\n    ++}\n    ++\n    + int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,\n    + \t\t\t\t       size_t update_idx,\n    + \t\t\t\t       enum ref_transaction_error err,\n     @@ refs.c: int ref_transaction_prepare(struct ref_transaction *transaction,\n      \t\treturn REF_TRANSACTION_ERROR_GENERIC;\n      \n    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n     -\t\t    const char *newref, const char *logmsg)\n     +static int refs_copy_or_rename_ref(struct ref_store *refs, const char *oldref,\n     +\t\t\t\t   const char *newref, const char *logmsg,\n    -+\t\t\t\t   int copy)\n    ++\t\t\t\t   bool copy)\n      {\n     -\tchar *msg;\n     -\tint retval;\n     +\tstruct ref_transaction *transaction = NULL;\n    ++\tstruct ref_copy_or_rename_update *operation = NULL;\n    ++\tstruct ref_update *destination_update;\n     +\tstruct object_id old_oid, new_oid;\n     +\tstruct strbuf new_target = STRBUF_INIT;\n     +\tstruct strbuf err = STRBUF_INIT;\n    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n     +\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n     +\tif (!transaction)\n     +\t\tgoto error;\n    -+\ttransaction->type = copy ? REF_TRANSACTION_TYPE_COPY :\n    -+\t\tREF_TRANSACTION_TYPE_RENAME;\n    -+\ttransaction->old_refname = xstrdup(oldref);\n    -+\ttransaction->new_refname = xstrdup(newref);\n    -+\ttransaction->logmsg = xstrdup(msg);\n    -+\toidcpy(&transaction->source_oid, &old_oid);\n    -+\n     +\tif (!copy && ref_transaction_delete(transaction, oldref, &old_oid, NULL,\n     +\t\t\t\t\t    REF_NO_DEREF, msg, &err))\n     +\t\tgoto error;\n    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n     +\t} else {\n     +\t\toidclr(&new_oid, refs->repo->hash_algo);\n     +\t}\n    -+\ttransaction->destination_exists = new_exists;\n    -+\tif (new_flags & REF_ISSYMREF)\n    -+\t\ttransaction->destination_target = xstrdup(new_target.buf);\n    -+\telse if (transaction->destination_exists)\n    -+\t\toidcpy(&transaction->destination_oid, &new_oid);\n    -+\n     +\tif (ref_transaction_update(transaction, newref, &old_oid,\n     +\t\t\t\t   (new_flags & REF_ISSYMREF) ? NULL : &new_oid,\n     +\t\t\t\t   NULL,\n    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n     +\t\t\t\t   NULL, &err))\n     +\t\tgoto error;\n     +\n    ++\tdestination_update = transaction->updates[transaction->nr - 1];\n    ++\tCALLOC_ARRAY(operation, 1);\n    ++\toperation->type = copy ? REF_UPDATE_COPY : REF_UPDATE_RENAME;\n    ++\toperation->old_refname = xstrdup(oldref);\n    ++\toperation->logmsg = xstrdup(msg);\n    ++\toidcpy(&operation->source_oid, &old_oid);\n    ++\toperation->destination_exists = new_exists;\n    ++\tif (new_flags & REF_ISSYMREF)\n    ++\t\toperation->destination_target = xstrdup(new_target.buf);\n    ++\telse if (operation->destination_exists)\n    ++\t\toidcpy(&operation->destination_oid, &new_oid);\n    ++\tdestination_update->copy_or_rename = operation;\n    ++\n     +\tif (ref_transaction_commit(transaction, &err))\n     +\t\tgoto error;\n     +\n    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n      \n      const char *ref_update_original_update_refname(struct ref_update *update)\n     \n    - ## refs.h ##\n    -@@ refs.h: enum ref_transaction_flag {\n    - \t * while rejecting updates which do not match the expected state.\n    - \t */\n    - \tREF_TRANSACTION_ALLOW_FAILURE = (1 << 1),\n    -+\n    -+\t/* Suppress hooks for an update nested in another transaction. */\n    -+\tREF_TRANSACTION_FLAG_SKIP_HOOK = (1 << 2),\n    - };\n    - \n    - /*\n    -\n      ## refs/debug.c ##\n     @@ refs/debug.c: static int debug_optimize_required(struct ref_store *ref_store,\n      \treturn res;\n    @@ refs/files-backend.c: static int refs_rename_ref_available(struct ref_store *ref\n     +};\n     +\n      static int files_copy_or_rename_ref(struct ref_store *ref_store,\n    - \t\t\t    const char *oldrefname, const char *newrefname,\n    +-\t\t\t    const char *oldrefname, const char *newrefname,\n     -\t\t\t    const char *logmsg, int copy)\n    -+\t\t\t    const char *logmsg, int copy,\n    -+\t\t\t    struct ref_transaction *transaction)\n    ++\t\t\t\t    struct ref_update *update,\n    ++\t\t\t\t    struct ref_transaction *transaction)\n      {\n      \tstruct files_ref_store *refs =\n     -\t\tfiles_downcast(ref_store, REF_STORE_WRITE, \"rename_ref\");\n     +\t\tfiles_downcast(ref_store, REF_STORE_WRITE,\n     +\t\t\t       \"ref_transaction_prepare\");\n    ++\tstruct ref_copy_or_rename_update *operation = update->copy_or_rename;\n    ++\tconst char *oldrefname = operation->old_refname;\n    ++\tconst char *newrefname = update->refname;\n    ++\tconst char *logmsg = operation->logmsg;\n    ++\tbool copy = operation->type == REF_UPDATE_COPY;\n      \tstruct object_id orig_oid;\n      \tint flag = 0, logmoved = 0;\n      \tstruct ref_lock *lock;\n    @@ refs/files-backend.c: static int files_copy_or_rename_ref(struct ref_store *ref_\n      \t\t\t\t    oldrefname);\n      \t\tgoto out;\n      \t}\n    -+\tif (!oideq(&orig_oid, &transaction->source_oid)) {\n    ++\tif (!oideq(&orig_oid, &operation->source_oid)) {\n     +\t\tret = error(\"refname %s is at %s but expected %s\",\n     +\t\t\t    oldrefname, oid_to_hex(&orig_oid),\n    -+\t\t\t    oid_to_hex(&transaction->source_oid));\n    ++\t\t\t    oid_to_hex(&operation->source_oid));\n     +\t\tgoto out;\n     +\t}\n      \tif (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {\n    @@ refs/files-backend.c: static int files_copy_or_rename_ref(struct ref_store *ref_\n     +\t\t\tgoto out;\n     +\t\t}\n     +\t}\n    -+\tif (destination_exists != transaction->destination_exists) {\n    ++\tif (destination_exists != operation->destination_exists) {\n     +\t\tret = error(\"refname %s changed while renaming\", newrefname);\n     +\t\tgoto out;\n     +\t}\n     +\tif (destination_exists) {\n     +\t\tif (destination_flags & REF_ISSYMREF) {\n    -+\t\t\tif (!transaction->destination_target ||\n    ++\t\t\tif (!operation->destination_target ||\n     +\t\t\t    strcmp(destination_target.buf,\n    -+\t\t\t\t   transaction->destination_target)) {\n    ++\t\t\t\t   operation->destination_target)) {\n     +\t\t\t\tret = error(\"refname %s changed while renaming\",\n     +\t\t\t\t\t    newrefname);\n     +\t\t\t\tgoto out;\n     +\t\t\t}\n    -+\t\t} else if (transaction->destination_target ||\n    ++\t\t} else if (operation->destination_target ||\n     +\t\t\t   !oideq(&destination_oid,\n    -+\t\t\t\t  &transaction->destination_oid)) {\n    ++\t\t\t\t  &operation->destination_oid)) {\n     +\t\t\tret = error(\"refname %s changed while renaming\", newrefname);\n     +\t\t\tgoto out;\n     +\t\t}\n    @@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref\n      \tstruct ref_transaction *packed_transaction = NULL;\n      \n      \tassert(err);\n    -+\tif (transaction->type != REF_TRANSACTION_TYPE_NORMAL)\n    -+\t\treturn files_copy_or_rename_ref(ref_store,\n    -+\t\t\t\ttransaction->old_refname,\n    -+\t\t\t\ttransaction->new_refname,\n    -+\t\t\t\ttransaction->logmsg,\n    -+\t\t\t\ttransaction->type == REF_TRANSACTION_TYPE_COPY,\n    -+\t\t\t\ttransaction);\n    ++\t{\n    ++\t\tstruct ref_update *operation =\n    ++\t\t\tref_transaction_copy_or_rename_update(transaction);\n    ++\n    ++\t\tif (operation)\n    ++\t\t\treturn files_copy_or_rename_ref(ref_store, operation,\n    ++\t\t\t\t\t\t\ttransaction);\n    ++\t}\n      \n      \tif (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n      \t\tgoto cleanup;\n    @@ refs/files-backend.c: static int files_transaction_finish(struct ref_store *ref_\n      \n      \n      \tassert(err);\n    -+\tif (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {\n    -+\t\tstruct files_copy_or_rename_transaction_data *data =\n    -+\t\t\ttransaction->backend_data;\n    -+\t\tint special_ret;\n    ++\t{\n    ++\t\tstruct ref_update *update =\n    ++\t\t\tref_transaction_copy_or_rename_update(transaction);\n     +\n    -+\t\tspecial_ret = commit_ref_update(refs, data->lock, &data->orig_oid,\n    -+\t\t\t\t\t\ttransaction->logmsg, 0, err);\n    -+\t\tif (special_ret) {\n    -+\t\t\terror(\"unable to write current sha1 into %s: %s\",\n    -+\t\t\t      transaction->new_refname, err->buf);\n    -+\t\t\tdata->lock = NULL;\n    -+\t\t\tfiles_transaction_abort(ref_store, transaction, err);\n    -+\t\t\treturn special_ret;\n    -+\t\t} else if (data->destination_log_backed_up) {\n    -+\t\t\tstruct strbuf path = STRBUF_INIT;\n    ++\t\tif (update) {\n    ++\t\t\tstruct ref_copy_or_rename_update *operation =\n    ++\t\t\t\tupdate->copy_or_rename;\n    ++\t\t\tstruct files_copy_or_rename_transaction_data *data =\n    ++\t\t\t\ttransaction->backend_data;\n    ++\t\t\tint special_ret;\n    ++\n    ++\t\t\tspecial_ret = commit_ref_update(refs, data->lock, &data->orig_oid,\n    ++\t\t\t\t\t\t\toperation->logmsg, 0, err);\n    ++\t\t\tif (special_ret) {\n    ++\t\t\t\terror(\"unable to write current sha1 into %s: %s\",\n    ++\t\t\t\t      update->refname, err->buf);\n    ++\t\t\t\tdata->lock = NULL;\n    ++\t\t\t\tfiles_transaction_abort(ref_store, transaction, err);\n    ++\t\t\t\treturn special_ret;\n    ++\t\t\t} else if (data->destination_log_backed_up) {\n    ++\t\t\t\tstruct strbuf path = STRBUF_INIT;\n     +\n    -+\t\t\tfiles_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);\n    -+\t\t\tif (unlink(path.buf) < 0 && errno != ENOENT)\n    -+\t\t\t\twarning_errno(\"unable to remove '%s'\", path.buf);\n    -+\t\t\tstrbuf_release(&path);\n    ++\t\t\t\tfiles_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);\n    ++\t\t\t\tif (unlink(path.buf) < 0 && errno != ENOENT)\n    ++\t\t\t\t\twarning_errno(\"unable to remove '%s'\", path.buf);\n    ++\t\t\t\tstrbuf_release(&path);\n    ++\t\t\t}\n    ++\t\t\tfree(data->destination_target);\n    ++\t\t\tfree(data);\n    ++\t\t\ttransaction->backend_data = NULL;\n    ++\t\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n    ++\t\t\treturn special_ret;\n     +\t\t}\n    -+\t\tfree(data->destination_target);\n    -+\t\tfree(data);\n    -+\t\ttransaction->backend_data = NULL;\n    -+\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n    -+\t\treturn special_ret;\n     +\t}\n      \n      \tif (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n    @@ refs/files-backend.c: static int files_transaction_finish(struct ref_store *ref_\n      \tstruct files_ref_store *refs =\n      \t\tfiles_downcast(ref_store, 0, \"ref_transaction_abort\");\n      \n    -+\tif (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {\n    -+\t\tstruct files_copy_or_rename_transaction_data *data =\n    -+\t\t\ttransaction->backend_data;\n    -+\t\tstruct strbuf new_log = STRBUF_INIT;\n    -+\t\tstruct strbuf destination_log = STRBUF_INIT;\n    -+\t\tstruct strbuf temporary_log = STRBUF_INIT;\n    -+\t\tstruct ref_transaction *restore_transaction = NULL;\n    -+\t\tstruct ref_lock *lock;\n    -+\t\tint ret = 0;\n    ++\t{\n    ++\t\tstruct ref_update *update =\n    ++\t\t\tref_transaction_copy_or_rename_update(transaction);\n     +\n    -+\t\tif (data->lock)\n    -+\t\t\tunlock_ref(data->lock);\n    -+\t\tif (transaction->type == REF_TRANSACTION_TYPE_RENAME) {\n    -+\t\t\tlock = lock_ref_oid_basic(refs, transaction->old_refname, err);\n    -+\t\t\tif (!lock ||\n    -+\t\t\t    write_ref_to_lockfile(refs, lock, &data->orig_oid, err) ||\n    -+\t\t\t    commit_ref_update(refs, lock, &data->orig_oid, NULL,\n    -+\t\t\t\t\t      REF_SKIP_CREATE_REFLOG, err))\n    -+\t\t\t\tret = -1;\n    -+\t\t}\n    ++\t\tif (update) {\n    ++\t\t\tstruct ref_copy_or_rename_update *operation =\n    ++\t\t\t\tupdate->copy_or_rename;\n    ++\t\t\tstruct files_copy_or_rename_transaction_data *data =\n    ++\t\t\t\ttransaction->backend_data;\n    ++\t\t\tstruct strbuf new_log = STRBUF_INIT;\n    ++\t\t\tstruct strbuf destination_log = STRBUF_INIT;\n    ++\t\t\tstruct strbuf temporary_log = STRBUF_INIT;\n    ++\t\t\tstruct ref_transaction *restore_transaction = NULL;\n    ++\t\t\tstruct ref_lock *lock;\n    ++\t\t\tint ret = 0;\n     +\n    -+\t\tif (data->logmoved) {\n    -+\t\t\tfiles_reflog_path(refs, &new_log, transaction->new_refname);\n    -+\t\t\tif (transaction->type == REF_TRANSACTION_TYPE_RENAME) {\n    -+\t\t\t\tfiles_reflog_path(refs, &temporary_log, TMP_RENAMED_LOG);\n    -+\t\t\t\tif (rename(new_log.buf, temporary_log.buf) < 0) {\n    -+\t\t\t\t\tstrbuf_addf(err, \"unable to restore logfile %s: %s\",\n    -+\t\t\t\t\t\t    transaction->old_refname, strerror(errno));\n    ++\t\t\tif (data->lock)\n    ++\t\t\t\tunlock_ref(data->lock);\n    ++\t\t\tif (operation->type == REF_UPDATE_RENAME) {\n    ++\t\t\t\tlock = lock_ref_oid_basic(refs, operation->old_refname, err);\n    ++\t\t\t\tif (!lock ||\n    ++\t\t\t\t    write_ref_to_lockfile(refs, lock, &data->orig_oid, err) ||\n    ++\t\t\t\t    commit_ref_update(refs, lock, &data->orig_oid, NULL,\n    ++\t\t\t\t\t\t      REF_SKIP_CREATE_REFLOG, err))\n     +\t\t\t\t\tret = -1;\n    -+\t\t\t\t} else {\n    -+\t\t\t\t\ttry_remove_empty_parents(refs,\n    -+\t\t\t\t\t\t\t transaction->new_refname,\n    -+\t\t\t\t\t\t\t REMOVE_EMPTY_PARENTS_REFLOG);\n    -+\t\t\t\t\tif (rename_tmp_log(refs,\n    -+\t\t\t\t\t\t\t   transaction->old_refname)) {\n    ++\t\t\t}\n    ++\n    ++\t\t\tif (data->logmoved) {\n    ++\t\t\t\tfiles_reflog_path(refs, &new_log, update->refname);\n    ++\t\t\t\tif (operation->type == REF_UPDATE_RENAME) {\n    ++\t\t\t\t\tfiles_reflog_path(refs, &temporary_log, TMP_RENAMED_LOG);\n    ++\t\t\t\t\tif (rename(new_log.buf, temporary_log.buf) < 0) {\n     +\t\t\t\t\t\tstrbuf_addf(err, \"unable to restore logfile %s: %s\",\n    -+\t\t\t\t\t\t\t    transaction->old_refname,\n    -+\t\t\t\t\t\t\t    strerror(errno));\n    ++\t\t\t\t\t\t\t    operation->old_refname, strerror(errno));\n     +\t\t\t\t\t\tret = -1;\n    ++\t\t\t\t\t} else {\n    ++\t\t\t\t\t\ttry_remove_empty_parents(refs,\n    ++\t\t\t\t\t\t\t\t\t update->refname,\n    ++\t\t\t\t\t\t\t\t\t REMOVE_EMPTY_PARENTS_REFLOG);\n    ++\t\t\t\t\t\tif (rename_tmp_log(refs,\n    ++\t\t\t\t\t\t\t\t   operation->old_refname)) {\n    ++\t\t\t\t\t\t\tstrbuf_addf(err, \"unable to restore logfile %s: %s\",\n    ++\t\t\t\t\t\t\t\t    operation->old_refname,\n    ++\t\t\t\t\t\t\t\t    strerror(errno));\n    ++\t\t\t\t\t\t\tret = -1;\n    ++\t\t\t\t\t\t}\n     +\t\t\t\t\t}\n    ++\t\t\t\t} else if (unlink(new_log.buf) < 0 && errno != ENOENT) {\n    ++\t\t\t\t\tstrbuf_addf(err, \"unable to remove logfile %s: %s\",\n    ++\t\t\t\t\t\t    update->refname, strerror(errno));\n    ++\t\t\t\t\tret = -1;\n     +\t\t\t\t}\n    -+\t\t\t} else if (unlink(new_log.buf) < 0 && errno != ENOENT) {\n    -+\t\t\t\tstrbuf_addf(err, \"unable to remove logfile %s: %s\",\n    -+\t\t\t\t\t    transaction->new_refname, strerror(errno));\n    -+\t\t\t\tret = -1;\n     +\t\t\t}\n    -+\t\t}\n    -+\t\tif (data->destination_log_backed_up) {\n    -+\t\t\tfiles_reflog_path(refs, &destination_log,\n    -+\t\t\t\t\t   TMP_RENAMED_LOG_DESTINATION);\n    -+\t\t\tif (rename(destination_log.buf, new_log.buf) < 0) {\n    -+\t\t\t\tstrbuf_addf(err, \"unable to restore logfile %s: %s\",\n    -+\t\t\t\t\t    transaction->new_refname, strerror(errno));\n    -+\t\t\t\tret = -1;\n    ++\t\t\tif (data->destination_log_backed_up) {\n    ++\t\t\t\tfiles_reflog_path(refs, &destination_log,\n    ++\t\t\t\t\t\t  TMP_RENAMED_LOG_DESTINATION);\n    ++\t\t\t\tif (rename(destination_log.buf, new_log.buf) < 0) {\n    ++\t\t\t\t\tstrbuf_addf(err, \"unable to restore logfile %s: %s\",\n    ++\t\t\t\t\t\t    update->refname, strerror(errno));\n    ++\t\t\t\t\tret = -1;\n    ++\t\t\t\t}\n     +\t\t\t}\n    -+\t\t}\n     +\n    -+\t\tif (transaction->type == REF_TRANSACTION_TYPE_RENAME &&\n    -+\t\t    data->destination_exists) {\n    -+\t\t\trestore_transaction = ref_store_transaction_begin(\n    ++\t\t\tif (operation->type == REF_UPDATE_RENAME &&\n    ++\t\t\t    data->destination_exists) {\n    ++\t\t\t\trestore_transaction = ref_store_transaction_begin(\n     +\t\t\t\t\t&refs->base, REF_TRANSACTION_FLAG_SKIP_HOOK, err);\n    -+\t\t\tif (!restore_transaction ||\n    -+\t\t\t    ref_transaction_update(restore_transaction,\n    -+\t\t\t\t\t\t   transaction->new_refname,\n    -+\t\t\t\t\t\t   data->destination_target ? NULL :\n    -+\t\t\t\t\t\t\t&data->destination_oid,\n    -+\t\t\t\t\t\t   NULL,\n    -+\t\t\t\t\t\t   data->destination_target,\n    -+\t\t\t\t\t\t   NULL,\n    -+\t\t\t\t\t\t   REF_NO_DEREF |\n    -+\t\t\t\t\t\t\tREF_SKIP_CREATE_REFLOG,\n    -+\t\t\t\t\t\t   NULL, err) ||\n    -+\t\t\t    ref_transaction_commit(restore_transaction, err))\n    -+\t\t\t\tret = -1;\n    -+\t\t\tref_transaction_free(restore_transaction);\n    -+\t\t}\n    ++\t\t\t\tif (!restore_transaction ||\n    ++\t\t\t\t    ref_transaction_update(restore_transaction,\n    ++\t\t\t\t\t\t\t   update->refname,\n    ++\t\t\t\t\t\t\t   data->destination_target ? NULL :\n    ++\t\t\t\t\t\t\t\t\t\t      &data->destination_oid,\n    ++\t\t\t\t\t\t\t   NULL,\n    ++\t\t\t\t\t\t\t   data->destination_target,\n    ++\t\t\t\t\t\t\t   NULL,\n    ++\t\t\t\t\t\t\t   REF_NO_DEREF |\n    ++\t\t\t\t\t\t\t\t   REF_SKIP_CREATE_REFLOG,\n    ++\t\t\t\t\t\t\t   NULL, err) ||\n    ++\t\t\t\t    ref_transaction_commit(restore_transaction, err))\n    ++\t\t\t\t\tret = -1;\n    ++\t\t\t\tref_transaction_free(restore_transaction);\n    ++\t\t\t}\n     +\n    -+\t\tstrbuf_release(&destination_log);\n    -+\t\tstrbuf_release(&temporary_log);\n    -+\t\tstrbuf_release(&new_log);\n    -+\t\tfree(data->destination_target);\n    -+\t\tfree(data);\n    -+\t\ttransaction->backend_data = NULL;\n    -+\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n    -+\t\treturn ret;\n    ++\t\t\tstrbuf_release(&destination_log);\n    ++\t\t\tstrbuf_release(&temporary_log);\n    ++\t\t\tstrbuf_release(&new_log);\n    ++\t\t\tfree(data->destination_target);\n    ++\t\t\tfree(data);\n    ++\t\t\ttransaction->backend_data = NULL;\n    ++\t\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n    ++\t\t\treturn ret;\n    ++\t\t}\n     +\t}\n     +\n      \tfiles_transaction_cleanup(refs, transaction);\n    @@ refs/packed-backend.c: struct ref_storage_be refs_be_packed = {\n      \t.read_raw_ref = packed_read_raw_ref,\n     \n      ## refs/refs-internal.h ##\n    +@@ refs/refs-internal.h: struct ref_update {\n    + \t */\n    + \tstruct ref_update *parent_update;\n    + \n    ++\t/*\n    ++\t * Copy and rename operations require backend-specific handling while\n    ++\t * still exposing their logical updates to transaction hooks. Keep that\n    ++\t * state on the destination update so it composes with other updates in\n    ++\t * the transaction instead of making copy or rename a transaction-wide\n    ++\t * property.\n    ++\t */\n    ++\tstruct ref_copy_or_rename_update *copy_or_rename;\n    ++\n    + \tconst char refname[FLEX_ARRAY];\n    + };\n    + \n    ++enum ref_copy_or_rename_type {\n    ++\tREF_UPDATE_RENAME,\n    ++\tREF_UPDATE_COPY,\n    ++};\n    ++\n    ++struct ref_copy_or_rename_update {\n    ++\tenum ref_copy_or_rename_type type;\n    ++\tchar *old_refname;\n    ++\tchar *logmsg;\n    ++\tstruct object_id source_oid;\n    ++\tstruct object_id destination_oid;\n    ++\tchar *destination_target;\n    ++\tunsigned int destination_exists:1;\n    ++};\n    ++\n    + int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,\n    + \t\t      struct object_id *oid, struct strbuf *referent,\n    + \t\t      unsigned int *type, int *failure_errno);\n     @@ refs/refs-internal.h: struct ref_update *ref_transaction_add_update(\n      \t\tconst char *committer_info,\n      \t\tconst char *msg);\n    @@ refs/refs-internal.h: struct ref_update *ref_transaction_add_update(\n      /*\n       * Transaction states.\n       *\n    -@@ refs/refs-internal.h: enum ref_transaction_state {\n    - \tREF_TRANSACTION_CLOSED   = 2\n    - };\n    - \n    -+enum ref_transaction_type {\n    -+\tREF_TRANSACTION_TYPE_NORMAL = 0,\n    -+\tREF_TRANSACTION_TYPE_RENAME,\n    -+\tREF_TRANSACTION_TYPE_COPY,\n    -+};\n    -+\n    - /*\n    -  * Data structure to hold indices of updates which were rejected, for batched\n    -  * reference updates. While the updates themselves hold the rejection error,\n     @@ refs/refs-internal.h: struct ref_transaction {\n    - \tvoid *backend_data;\n    - \tunsigned int flags;\n      \tuint64_t max_index;\n    -+\n    -+\t/*\n    -+\t * Rename and copy operations need backend-specific reflog handling.\n    -+\t * Their logical updates still live in `updates`, so hooks see the\n    -+\t * operation like any other reference transaction. The fields below\n    -+\t * retain the state that backends verify after taking their locks.\n    -+\t */\n    -+\tenum ref_transaction_type type;\n    -+\tchar *old_refname;\n    -+\tchar *new_refname;\n    -+\tchar *logmsg;\n    -+\tstruct object_id source_oid;\n    -+\tstruct object_id destination_oid;\n    -+\tchar *destination_target;\n    -+\tunsigned int destination_exists:1;\n      };\n      \n    ++/* Suppress hooks for a transaction nested inside another refs operation. */\n    ++#define REF_TRANSACTION_FLAG_SKIP_HOOK (1 << 2)\n    ++\n    ++struct ref_update *ref_transaction_copy_or_rename_update(\n    ++\tstruct ref_transaction *transaction);\n    ++\n      /*\n    +  * Check for entries in extras that are within the specified\n    +  * directory, where dirname is a reference directory name including\n     @@ refs/refs-internal.h: typedef int optimize_required_fn(struct ref_store *ref_store,\n      \t\t\t\t struct refs_optimize_opts *opts,\n      \t\t\t\t bool *required);\n    @@ refs/reftable-backend.c: static int reftable_be_transaction_prepare(struct ref_s\n      \tsize_t i;\n      \tint ret;\n      \n    -+\tif (transaction->type != REF_TRANSACTION_TYPE_NORMAL)\n    ++\tif (ref_transaction_copy_or_rename_update(transaction))\n     +\t\treturn reftable_be_copy_or_rename_prepare(ref_store, transaction,\n     +\t\t\t\t\t\t\t   err);\n     +\n    @@ refs/reftable-backend.c: static int reftable_be_transaction_abort(struct ref_sto\n     -\tstruct reftable_transaction_data *tx_data = transaction->backend_data;\n     +\tstruct reftable_transaction_data *tx_data;\n     +\n    -+\tif (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {\n    ++\tif (ref_transaction_copy_or_rename_update(transaction)) {\n     +\t\tstruct reftable_copy_or_rename_transaction_data *data =\n     +\t\t\ttransaction->backend_data;\n     +\n    @@ refs/reftable-backend.c: static int reftable_be_transaction_finish(struct ref_st\n     +\tstruct reftable_transaction_data *tx_data;\n      \tint ret = 0;\n      \n    -+\tif (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {\n    ++\tif (ref_transaction_copy_or_rename_update(transaction)) {\n     +\t\tstruct reftable_copy_or_rename_transaction_data *data =\n     +\t\t\ttransaction->backend_data;\n     +\t\tint special_ret = reftable_addition_commit(data->addition);\n    @@ refs/reftable-backend.c: struct write_create_symref_arg {\n      \tconst char *newname;\n      \tconst char *logmsg;\n      \tint delete_old;\n    -+\tstruct ref_transaction *transaction;\n    ++\tstruct ref_copy_or_rename_update *operation;\n      };\n      \n      static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n     +\t\tret = -1;\n      \t\tgoto done;\n      \t}\n    -+\tif (arg->transaction) {\n    ++\tif (arg->operation) {\n     +\t\tstruct object_id oid;\n     +\n     +\t\tif (old_ref.value_type == REFTABLE_REF_VAL2)\n    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n     +\t\telse\n     +\t\t\toidread(&oid, old_ref.value.val1,\n     +\t\t\t\targ->refs->base.repo->hash_algo);\n    -+\t\tif (!oideq(&oid, &arg->transaction->source_oid)) {\n    ++\t\tif (!oideq(&oid, &arg->operation->source_oid)) {\n     +\t\t\tstrbuf_addf(arg->err,\n     +\t\t\t\t    _(\"refname %s is at %s but expected %s\"),\n     +\t\t\t\t    arg->oldname, oid_to_hex(&oid),\n    -+\t\t\t\t    oid_to_hex(&arg->transaction->source_oid));\n    ++\t\t\t\t    oid_to_hex(&arg->operation->source_oid));\n     +\t\t\tret = -1;\n     +\t\t\tgoto done;\n     +\t\t}\n    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n     +\t\t\t\t\t      &destination_ref);\n     +\t\tif (ret < 0)\n     +\t\t\tgoto done;\n    -+\t\tif (arg->transaction->destination_exists != !ret) {\n    ++\t\tif (arg->operation->destination_exists != !ret) {\n     +\t\t\tstrbuf_addf(arg->err,\n     +\t\t\t\t    _(\"refname %s changed while renaming\"),\n     +\t\t\t\t    arg->newname);\n    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n     +\t\t}\n     +\t\tif (!ret) {\n     +\t\t\tif (destination_ref.value_type == REFTABLE_REF_SYMREF) {\n    -+\t\t\t\tif (!arg->transaction->destination_target ||\n    ++\t\t\t\tif (!arg->operation->destination_target ||\n     +\t\t\t\t    strcmp(destination_ref.value.symref,\n    -+\t\t\t\t\t   arg->transaction->destination_target)) {\n    ++\t\t\t\t\t   arg->operation->destination_target)) {\n     +\t\t\t\t\tstrbuf_addf(arg->err,\n     +\t\t\t\t\t\t    _(\"refname %s changed while renaming\"),\n     +\t\t\t\t\t\t    arg->newname);\n    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n     +\t\t\t\telse\n     +\t\t\t\t\toidread(&oid, destination_ref.value.val1,\n     +\t\t\t\t\t\targ->refs->base.repo->hash_algo);\n    -+\t\t\t\tif (arg->transaction->destination_target ||\n    -+\t\t\t\t    !oideq(&oid, &arg->transaction->destination_oid)) {\n    ++\t\t\t\tif (arg->operation->destination_target ||\n    ++\t\t\t\t    !oideq(&oid, &arg->operation->destination_oid)) {\n     +\t\t\t\t\tstrbuf_addf(arg->err,\n     +\t\t\t\t\t\t    _(\"refname %s changed while renaming\"),\n     +\t\t\t\t\t\t    arg->newname);\n    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n     +\t\treftable_be_downcast(ref_store, REF_STORE_WRITE,\n     +\t\t\t\t     \"ref_transaction_prepare\");\n     +\tstruct reftable_copy_or_rename_transaction_data *data = NULL;\n    ++\tstruct ref_update *update =\n    ++\t\tref_transaction_copy_or_rename_update(transaction);\n    ++\tstruct ref_copy_or_rename_update *operation = update->copy_or_rename;\n      \tstruct write_copy_arg arg = {\n      \t\t.refs = refs,\n     -\t\t.oldname = oldrefname,\n    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n     -\t\t.logmsg = logmsg,\n     -\t\t.delete_old = 1,\n     +\t\t.err = err,\n    -+\t\t.oldname = transaction->old_refname,\n    -+\t\t.newname = transaction->new_refname,\n    -+\t\t.logmsg = transaction->logmsg,\n    -+\t\t.delete_old = transaction->type == REF_TRANSACTION_TYPE_RENAME,\n    -+\t\t.transaction = transaction,\n    ++\t\t.oldname = operation->old_refname,\n    ++\t\t.newname = update->refname,\n    ++\t\t.logmsg = operation->logmsg,\n    ++\t\t.delete_old = operation->type == REF_UPDATE_RENAME,\n    ++\t\t.operation = operation,\n      \t};\n      \tint ret;\n      \n    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n      \t\tgoto done;\n     -\n     -\tret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);\n    -+\tret = backend_for(&arg.be, refs, transaction->new_refname,\n    ++\tret = backend_for(&arg.be, refs, update->refname,\n     +\t\t\t  &arg.newname, 1);\n      \tif (ret)\n      \t\tgoto done;\n\n refs.c                           | 161 ++++++++++++++---\n refs/debug.c                     |  25 ---\n refs/files-backend.c             | 297 +++++++++++++++++++++++++++----\n refs/packed-backend.c            |   2 -\n refs/refs-internal.h             |  47 +++--\n refs/reftable-backend.c          | 197 +++++++++++++++-----\n t/t1416-ref-transaction-hooks.sh | 142 +++++++++++++++\n 7 files changed, 732 insertions(+), 139 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 92d5df5b7..f036ae4b9 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1004,15 +1004,17 @@ long get_files_ref_lock_timeout_ms(struct repository *repo)\n \treturn timeout_ms;\n }\n \n-int refs_delete_ref(struct ref_store *refs, const char *msg,\n-\t\t    const char *refname,\n-\t\t    const struct object_id *old_oid,\n-\t\t    unsigned int flags)\n+int refs_delete_ref_with_transaction_flags(struct ref_store *refs,\n+\t\t\t\t\t   const char *msg,\n+\t\t\t\t\t   const char *refname,\n+\t\t\t\t\t   const struct object_id *old_oid,\n+\t\t\t\t\t   unsigned int flags,\n+\t\t\t\t\t   unsigned int transaction_flags)\n {\n \tstruct ref_transaction *transaction;\n \tstruct strbuf err = STRBUF_INIT;\n \n-\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n+\ttransaction = ref_store_transaction_begin(refs, transaction_flags, &err);\n \tif (!transaction ||\n \t    ref_transaction_delete(transaction, refname, old_oid,\n \t\t\t\t   NULL, flags, msg, &err) ||\n@@ -1027,6 +1029,15 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,\n \treturn 0;\n }\n \n+int refs_delete_ref(struct ref_store *refs, const char *msg,\n+\t\t    const char *refname,\n+\t\t    const struct object_id *old_oid,\n+\t\t    unsigned int flags)\n+{\n+\treturn refs_delete_ref_with_transaction_flags(refs, msg, refname,\n+\t\t\t\t\t\t      old_oid, flags, 0);\n+}\n+\n static void copy_reflog_msg(struct strbuf *sb, const char *msg)\n {\n \tchar c;\n@@ -1256,11 +1267,20 @@ void ref_transaction_free(struct ref_transaction *transaction)\n \t}\n \n \tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_copy_or_rename_update *operation =\n+\t\t\ttransaction->updates[i]->copy_or_rename;\n+\n \t\tfree(transaction->updates[i]->msg);\n \t\tfree(transaction->updates[i]->committer_info);\n \t\tfree((char *)transaction->updates[i]->new_target);\n \t\tfree((char *)transaction->updates[i]->old_target);\n \t\tfree((char *)transaction->updates[i]->rejection_details);\n+\t\tif (operation) {\n+\t\t\tfree(operation->old_refname);\n+\t\t\tfree(operation->logmsg);\n+\t\t\tfree(operation->destination_target);\n+\t\t\tfree(operation);\n+\t\t}\n \t\tfree(transaction->updates[i]);\n \t}\n \n@@ -1273,6 +1293,23 @@ void ref_transaction_free(struct ref_transaction *transaction)\n \tfree(transaction);\n }\n \n+struct ref_update *ref_transaction_copy_or_rename_update(\n+\tstruct ref_transaction *transaction)\n+{\n+\tstruct ref_update *operation = NULL;\n+\tsize_t i;\n+\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tif (!transaction->updates[i]->copy_or_rename)\n+\t\t\tcontinue;\n+\t\tif (operation)\n+\t\t\tBUG(\"multiple copy or rename updates in one transaction\");\n+\t\toperation = transaction->updates[i];\n+\t}\n+\n+\treturn operation;\n+}\n+\n int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,\n \t\t\t\t       size_t update_idx,\n \t\t\t\t       enum ref_transaction_error err,\n@@ -2710,7 +2747,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,\n \t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \n \t/* Preparing checks before locking references */\n-\tret = run_transaction_hook(transaction, \"preparing\");\n+\tret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :\n+\t\trun_transaction_hook(transaction, \"preparing\");\n \tif (ret) {\n \t\tref_transaction_abort(transaction, err);\n \t\tdie(_(abort_by_ref_transaction_hook), \"preparing\");\n@@ -2720,7 +2758,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,\n \tif (ret)\n \t\treturn ret;\n \n-\tret = run_transaction_hook(transaction, \"prepared\");\n+\tret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :\n+\t\trun_transaction_hook(transaction, \"prepared\");\n \tif (ret) {\n \t\tref_transaction_abort(transaction, err);\n \t\tdie(_(abort_by_ref_transaction_hook), \"prepared\");\n@@ -2750,7 +2789,8 @@ int ref_transaction_abort(struct ref_transaction *transaction,\n \t\tbreak;\n \t}\n \n-\trun_transaction_hook(transaction, \"aborted\");\n+\tif (!(transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK))\n+\t\trun_transaction_hook(transaction, \"aborted\");\n \n \tref_transaction_free(transaction);\n \treturn ret;\n@@ -2781,7 +2821,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n \t}\n \n \tret = refs->be->transaction_finish(refs, transaction, err);\n-\tif (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL))\n+\tif (!ret && !(transaction->flags & (REF_TRANSACTION_FLAG_INITIAL |\n+\t\t\t\t\t REF_TRANSACTION_FLAG_SKIP_HOOK)))\n \t\trun_transaction_hook(transaction, \"committed\");\n \treturn ret;\n }\n@@ -3123,28 +3164,102 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n \treturn ret;\n }\n \n-int refs_rename_ref(struct ref_store *refs, const char *oldref,\n-\t\t    const char *newref, const char *logmsg)\n+static int refs_copy_or_rename_ref(struct ref_store *refs, const char *oldref,\n+\t\t\t\t   const char *newref, const char *logmsg,\n+\t\t\t\t   bool copy)\n {\n-\tchar *msg;\n-\tint retval;\n+\tstruct ref_transaction *transaction = NULL;\n+\tstruct ref_copy_or_rename_update *operation = NULL;\n+\tstruct ref_update *destination_update;\n+\tstruct object_id old_oid, new_oid;\n+\tstruct strbuf new_target = STRBUF_INIT;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tchar *msg = normalize_reflog_message(logmsg);\n+\tint old_flags, new_flags = 0, new_exists = 0, ret = 1;\n \n-\tmsg = normalize_reflog_message(logmsg);\n-\tretval = refs->be->rename_ref(refs, oldref, newref, msg);\n+\tif (!strcmp(oldref, newref)) {\n+\t\tret = 0;\n+\t\tgoto out;\n+\t}\n+\n+\tif (!refs_resolve_ref_unsafe(refs, oldref,\n+\t\t\t\t     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t     &old_oid, &old_flags)) {\n+\t\terror(\"refname %s not found\", oldref);\n+\t\tgoto out;\n+\t}\n+\tif (old_flags & REF_ISSYMREF) {\n+\t\terror(\"refname %s is a symbolic ref, %s it is not supported\",\n+\t\t      oldref, copy ? \"copying\" : \"renaming\");\n+\t\tgoto out;\n+\t}\n+\n+\ttransaction = ref_store_transaction_begin(refs, 0, &err);\n+\tif (!transaction)\n+\t\tgoto error;\n+\tif (!copy && ref_transaction_delete(transaction, oldref, &old_oid, NULL,\n+\t\t\t\t\t    REF_NO_DEREF, msg, &err))\n+\t\tgoto error;\n+\n+\tif (refs_resolve_ref_unsafe(refs, newref,\n+\t\t\t\t    RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t    &new_oid, &new_flags)) {\n+\t\tnew_exists = 1;\n+\t\tif ((new_flags & REF_ISSYMREF) &&\n+\t\t    refs_read_symbolic_ref(refs, newref, &new_target) < 0) {\n+\t\t\tstrbuf_addf(&err, \"unable to read symbolic ref %s\", newref);\n+\t\t\tgoto error;\n+\t\t}\n+\t} else {\n+\t\toidclr(&new_oid, refs->repo->hash_algo);\n+\t}\n+\tif (ref_transaction_update(transaction, newref, &old_oid,\n+\t\t\t\t   (new_flags & REF_ISSYMREF) ? NULL : &new_oid,\n+\t\t\t\t   NULL,\n+\t\t\t\t   (new_flags & REF_ISSYMREF) ? new_target.buf : NULL,\n+\t\t\t\t   REF_NO_DEREF | REF_SKIP_CREATE_REFLOG,\n+\t\t\t\t   NULL, &err))\n+\t\tgoto error;\n+\n+\tdestination_update = transaction->updates[transaction->nr - 1];\n+\tCALLOC_ARRAY(operation, 1);\n+\toperation->type = copy ? REF_UPDATE_COPY : REF_UPDATE_RENAME;\n+\toperation->old_refname = xstrdup(oldref);\n+\toperation->logmsg = xstrdup(msg);\n+\toidcpy(&operation->source_oid, &old_oid);\n+\toperation->destination_exists = new_exists;\n+\tif (new_flags & REF_ISSYMREF)\n+\t\toperation->destination_target = xstrdup(new_target.buf);\n+\telse if (operation->destination_exists)\n+\t\toidcpy(&operation->destination_oid, &new_oid);\n+\tdestination_update->copy_or_rename = operation;\n+\n+\tif (ref_transaction_commit(transaction, &err))\n+\t\tgoto error;\n+\n+\tret = 0;\n+\tgoto out;\n+\n+error:\n+\terror(\"%s\", err.buf);\n+out:\n+\tref_transaction_free(transaction);\n+\tstrbuf_release(&new_target);\n+\tstrbuf_release(&err);\n \tfree(msg);\n-\treturn retval;\n+\treturn ret;\n }\n \n-int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,\n+int refs_rename_ref(struct ref_store *refs, const char *oldref,\n \t\t    const char *newref, const char *logmsg)\n {\n-\tchar *msg;\n-\tint retval;\n+\treturn refs_copy_or_rename_ref(refs, oldref, newref, logmsg, 0);\n+}\n \n-\tmsg = normalize_reflog_message(logmsg);\n-\tretval = refs->be->copy_ref(refs, oldref, newref, msg);\n-\tfree(msg);\n-\treturn retval;\n+int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,\n+\t\t    const char *newref, const char *logmsg)\n+{\n+\treturn refs_copy_or_rename_ref(refs, oldref, newref, logmsg, 1);\n }\n \n const char *ref_update_original_update_refname(struct ref_update *update)\ndiff --git a/refs/debug.c b/refs/debug.c\nindex 639db0f26..87b84e767 100644\n--- a/refs/debug.c\n+++ b/refs/debug.c\n@@ -143,28 +143,6 @@ static int debug_optimize_required(struct ref_store *ref_store,\n \treturn res;\n }\n \n-static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,\n-\t\t\t    const char *newref, const char *logmsg)\n-{\n-\tstruct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;\n-\tint res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref,\n-\t\t\t\t\t      logmsg);\n-\ttrace_printf_key(&trace_refs, \"rename_ref: %s -> %s \\\"%s\\\": %d\\n\", oldref, newref,\n-\t\tlogmsg, res);\n-\treturn res;\n-}\n-\n-static int debug_copy_ref(struct ref_store *ref_store, const char *oldref,\n-\t\t\t  const char *newref, const char *logmsg)\n-{\n-\tstruct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;\n-\tint res =\n-\t\tdrefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg);\n-\ttrace_printf_key(&trace_refs, \"copy_ref: %s -> %s \\\"%s\\\": %d\\n\", oldref, newref,\n-\t\tlogmsg, res);\n-\treturn res;\n-}\n-\n struct debug_ref_iterator {\n \tstruct ref_iterator base;\n \tstruct ref_iterator *iter;\n@@ -453,9 +431,6 @@ struct ref_storage_be refs_be_debug = {\n \t.optimize = debug_optimize,\n \t.optimize_required = debug_optimize_required,\n \n-\t.rename_ref = debug_rename_ref,\n-\t.copy_ref = debug_copy_ref,\n-\n \t.iterator_begin = debug_ref_iterator_begin,\n \t.read_raw_ref = debug_read_raw_ref,\n \t.read_symbolic_ref = debug_read_symbolic_ref,\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 71628550f..c28228116 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1594,6 +1594,7 @@ static int files_optimize_required(struct ref_store *ref_store,\n  * live into logs/refs.\n  */\n #define TMP_RENAMED_LOG  \"refs/.tmp-renamed-log\"\n+#define TMP_RENAMED_LOG_DESTINATION \"refs/.tmp-renamed-log-destination\"\n \n struct rename_cb {\n \tconst char *tmp_renamed_log;\n@@ -1685,12 +1686,28 @@ static int refs_rename_ref_available(struct ref_store *refs,\n \treturn ok;\n }\n \n+struct files_copy_or_rename_transaction_data {\n+\tstruct ref_lock *lock;\n+\tstruct object_id orig_oid;\n+\tstruct object_id destination_oid;\n+\tchar *destination_target;\n+\tint logmoved;\n+\tint destination_exists;\n+\tint destination_log_backed_up;\n+};\n+\n static int files_copy_or_rename_ref(struct ref_store *ref_store,\n-\t\t\t    const char *oldrefname, const char *newrefname,\n-\t\t\t    const char *logmsg, int copy)\n+\t\t\t\t    struct ref_update *update,\n+\t\t\t\t    struct ref_transaction *transaction)\n {\n \tstruct files_ref_store *refs =\n-\t\tfiles_downcast(ref_store, REF_STORE_WRITE, \"rename_ref\");\n+\t\tfiles_downcast(ref_store, REF_STORE_WRITE,\n+\t\t\t       \"ref_transaction_prepare\");\n+\tstruct ref_copy_or_rename_update *operation = update->copy_or_rename;\n+\tconst char *oldrefname = operation->old_refname;\n+\tconst char *newrefname = update->refname;\n+\tconst char *logmsg = operation->logmsg;\n+\tbool copy = operation->type == REF_UPDATE_COPY;\n \tstruct object_id orig_oid;\n \tint flag = 0, logmoved = 0;\n \tstruct ref_lock *lock;\n@@ -1698,12 +1715,19 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \tstruct strbuf sb_oldref = STRBUF_INIT;\n \tstruct strbuf sb_newref = STRBUF_INIT;\n \tstruct strbuf tmp_renamed_log = STRBUF_INIT;\n+\tstruct strbuf tmp_destination_log = STRBUF_INIT;\n+\tstruct strbuf destination_target = STRBUF_INIT;\n \tint log, ret;\n+\tint destination_exists = 0, destination_flags = 0;\n+\tint destination_log_backed_up = 0;\n+\tstruct object_id destination_oid;\n+\tstruct files_copy_or_rename_transaction_data *data;\n \tstruct strbuf err = STRBUF_INIT;\n \n \tfiles_reflog_path(refs, &sb_oldref, oldrefname);\n \tfiles_reflog_path(refs, &sb_newref, newrefname);\n \tfiles_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);\n+\tfiles_reflog_path(refs, &tmp_destination_log, TMP_RENAMED_LOG_DESTINATION);\n \n \tlog = !lstat(sb_oldref.buf, &loginfo);\n \tif (log && S_ISLNK(loginfo.st_mode)) {\n@@ -1727,11 +1751,67 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \t\t\t\t    oldrefname);\n \t\tgoto out;\n \t}\n+\tif (!oideq(&orig_oid, &operation->source_oid)) {\n+\t\tret = error(\"refname %s is at %s but expected %s\",\n+\t\t\t    oldrefname, oid_to_hex(&orig_oid),\n+\t\t\t    oid_to_hex(&operation->source_oid));\n+\t\tgoto out;\n+\t}\n \tif (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {\n \t\tret = 1;\n \t\tgoto out;\n \t}\n \n+\tif (refs_resolve_ref_unsafe(&refs->base, newrefname,\n+\t\t\t\t    RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t    &destination_oid, &destination_flags)) {\n+\t\tdestination_exists = 1;\n+\t\tif ((destination_flags & REF_ISSYMREF) &&\n+\t\t    refs_read_symbolic_ref(&refs->base, newrefname,\n+\t\t\t\t\t   &destination_target) < 0) {\n+\t\t\tret = error(\"unable to read symbolic ref %s\", newrefname);\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\tif (destination_exists != operation->destination_exists) {\n+\t\tret = error(\"refname %s changed while renaming\", newrefname);\n+\t\tgoto out;\n+\t}\n+\tif (destination_exists) {\n+\t\tif (destination_flags & REF_ISSYMREF) {\n+\t\t\tif (!operation->destination_target ||\n+\t\t\t    strcmp(destination_target.buf,\n+\t\t\t\t   operation->destination_target)) {\n+\t\t\t\tret = error(\"refname %s changed while renaming\",\n+\t\t\t\t\t    newrefname);\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t} else if (operation->destination_target ||\n+\t\t\t   !oideq(&destination_oid,\n+\t\t\t\t  &operation->destination_oid)) {\n+\t\t\tret = error(\"refname %s changed while renaming\", newrefname);\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tif (!lstat(sb_newref.buf, &loginfo)) {\n+\t\tif (S_ISLNK(loginfo.st_mode)) {\n+\t\t\tret = error(\"reflog for %s is a symlink\", newrefname);\n+\t\t\tgoto out;\n+\t\t}\n+\t\tif (S_ISREG(loginfo.st_mode)) {\n+\t\t\tif (copy_file(refs->base.repo, tmp_destination_log.buf,\n+\t\t\t\t      sb_newref.buf, 0644)) {\n+\t\t\t\tif (errno != EEXIST)\n+\t\t\t\t\tunlink(tmp_destination_log.buf);\n+\t\t\t\tret = error(\"unable to back up logfile logs/%s: %s\",\n+\t\t\t\t\t    newrefname, strerror(errno));\n+\t\t\t\tgoto out;\n+\t\t\t}\n+\t\t\tdestination_log_backed_up = 1;\n+\t\t}\n+\t}\n+\n \tif (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {\n \t\tret = error(\"unable to move logfile logs/%s to logs/\"TMP_RENAMED_LOG\": %s\",\n \t\t\t    oldrefname, strerror(errno));\n@@ -1744,8 +1824,10 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \t\tgoto out;\n \t}\n \n-\tif (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,\n-\t\t\t    &orig_oid, REF_NO_DEREF)) {\n+\tif (!copy && refs_delete_ref_with_transaction_flags(&refs->base, logmsg,\n+\t\t\t\t\t\t\t oldrefname, &orig_oid,\n+\t\t\t\t\t\t\t REF_NO_DEREF,\n+\t\t\t\t\t\t\t REF_TRANSACTION_FLAG_SKIP_HOOK)) {\n \t\terror(\"unable to delete old %s\", oldrefname);\n \t\tgoto rollback;\n \t}\n@@ -1760,8 +1842,9 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \tif (!copy && refs_resolve_ref_unsafe(&refs->base, newrefname,\n \t\t\t\t\t     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n \t\t\t\t\t     NULL, NULL) &&\n-\t    refs_delete_ref(&refs->base, NULL, newrefname,\n-\t\t\t    NULL, REF_NO_DEREF)) {\n+\t    refs_delete_ref_with_transaction_flags(&refs->base, NULL, newrefname,\n+\t\t\t\t\t\t     NULL, REF_NO_DEREF,\n+\t\t\t\t\t\t     REF_TRANSACTION_FLAG_SKIP_HOOK)) {\n \t\tif (errno == EISDIR) {\n \t\t\tstruct strbuf path = STRBUF_INIT;\n \t\t\tint result;\n@@ -1796,13 +1879,25 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \t}\n \toidcpy(&lock->old_oid, &orig_oid);\n \n-\tif (write_ref_to_lockfile(refs, lock, &orig_oid, &err) ||\n-\t    commit_ref_update(refs, lock, &orig_oid, logmsg, 0, &err)) {\n+\tif (write_ref_to_lockfile(refs, lock, &orig_oid, &err)) {\n \t\terror(\"unable to write current sha1 into %s: %s\", newrefname, err.buf);\n \t\tstrbuf_release(&err);\n \t\tgoto rollback;\n \t}\n \n+\tCALLOC_ARRAY(data, 1);\n+\tdata->lock = lock;\n+\toidcpy(&data->orig_oid, &orig_oid);\n+\tdata->logmoved = logmoved;\n+\tdata->destination_exists = destination_exists;\n+\tdata->destination_log_backed_up = destination_log_backed_up;\n+\tif (destination_exists && !(destination_flags & REF_ISSYMREF))\n+\t\toidcpy(&data->destination_oid, &destination_oid);\n+\tif (destination_flags & REF_ISSYMREF)\n+\t\tdata->destination_target = strbuf_detach(&destination_target, NULL);\n+\ttransaction->backend_data = data;\n+\ttransaction->state = REF_TRANSACTION_PREPARED;\n+\n \tret = 0;\n \tgoto out;\n \n@@ -1821,38 +1916,40 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n \t}\n \n  rollbacklog:\n-\tif (logmoved && rename(sb_newref.buf, sb_oldref.buf))\n-\t\terror(\"unable to restore logfile %s from %s: %s\",\n-\t\t\toldrefname, newrefname, strerror(errno));\n+\tif (logmoved) {\n+\t\tif (rename(sb_newref.buf, tmp_renamed_log.buf)) {\n+\t\t\terror(\"unable to restore logfile %s from %s: %s\",\n+\t\t\t      oldrefname, newrefname, strerror(errno));\n+\t\t} else {\n+\t\t\ttry_remove_empty_parents(refs, newrefname,\n+\t\t\t\t\t\t REMOVE_EMPTY_PARENTS_REFLOG);\n+\t\t\tif (rename_tmp_log(refs, oldrefname))\n+\t\t\t\terror(\"unable to restore logfile %s from logs/\"\n+\t\t\t\t      TMP_RENAMED_LOG \": %s\",\n+\t\t\t\t      oldrefname, strerror(errno));\n+\t\t}\n+\t}\n \tif (!logmoved && log &&\n \t    rename(tmp_renamed_log.buf, sb_oldref.buf))\n \t\terror(\"unable to restore logfile %s from logs/\"TMP_RENAMED_LOG\": %s\",\n \t\t\toldrefname, strerror(errno));\n+\tif (destination_log_backed_up &&\n+\t    rename(tmp_destination_log.buf, sb_newref.buf))\n+\t\terror(\"unable to restore logfile %s: %s\",\n+\t\t      newrefname, strerror(errno));\n \tret = 1;\n  out:\n+\tif (ret && destination_log_backed_up)\n+\t\tunlink(tmp_destination_log.buf);\n \tstrbuf_release(&sb_newref);\n \tstrbuf_release(&sb_oldref);\n \tstrbuf_release(&tmp_renamed_log);\n+\tstrbuf_release(&tmp_destination_log);\n+\tstrbuf_release(&destination_target);\n \n \treturn ret;\n }\n \n-static int files_rename_ref(struct ref_store *ref_store,\n-\t\t\t    const char *oldrefname, const char *newrefname,\n-\t\t\t    const char *logmsg)\n-{\n-\treturn files_copy_or_rename_ref(ref_store, oldrefname,\n-\t\t\t\t newrefname, logmsg, 0);\n-}\n-\n-static int files_copy_ref(struct ref_store *ref_store,\n-\t\t\t    const char *oldrefname, const char *newrefname,\n-\t\t\t    const char *logmsg)\n-{\n-\treturn files_copy_or_rename_ref(ref_store, oldrefname,\n-\t\t\t\t newrefname, logmsg, 1);\n-}\n-\n static int close_ref_gently(struct ref_lock *lock)\n {\n \tif (close_lock_file_gently(&lock->lk))\n@@ -2962,6 +3059,14 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \tstruct ref_transaction *packed_transaction = NULL;\n \n \tassert(err);\n+\t{\n+\t\tstruct ref_update *operation =\n+\t\t\tref_transaction_copy_or_rename_update(transaction);\n+\n+\t\tif (operation)\n+\t\t\treturn files_copy_or_rename_ref(ref_store, operation,\n+\t\t\t\t\t\t\ttransaction);\n+\t}\n \n \tif (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n \t\tgoto cleanup;\n@@ -3318,6 +3423,10 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n \treturn ret;\n }\n \n+static int files_transaction_abort(struct ref_store *ref_store,\n+\t\t\t\t   struct ref_transaction *transaction,\n+\t\t\t\t   struct strbuf *err);\n+\n static int files_transaction_finish(struct ref_store *ref_store,\n \t\t\t\t    struct ref_transaction *transaction,\n \t\t\t\t    struct strbuf *err)\n@@ -3333,6 +3442,40 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \n \n \tassert(err);\n+\t{\n+\t\tstruct ref_update *update =\n+\t\t\tref_transaction_copy_or_rename_update(transaction);\n+\n+\t\tif (update) {\n+\t\t\tstruct ref_copy_or_rename_update *operation =\n+\t\t\t\tupdate->copy_or_rename;\n+\t\t\tstruct files_copy_or_rename_transaction_data *data =\n+\t\t\t\ttransaction->backend_data;\n+\t\t\tint special_ret;\n+\n+\t\t\tspecial_ret = commit_ref_update(refs, data->lock, &data->orig_oid,\n+\t\t\t\t\t\t\toperation->logmsg, 0, err);\n+\t\t\tif (special_ret) {\n+\t\t\t\terror(\"unable to write current sha1 into %s: %s\",\n+\t\t\t\t      update->refname, err->buf);\n+\t\t\t\tdata->lock = NULL;\n+\t\t\t\tfiles_transaction_abort(ref_store, transaction, err);\n+\t\t\t\treturn special_ret;\n+\t\t\t} else if (data->destination_log_backed_up) {\n+\t\t\t\tstruct strbuf path = STRBUF_INIT;\n+\n+\t\t\t\tfiles_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);\n+\t\t\t\tif (unlink(path.buf) < 0 && errno != ENOENT)\n+\t\t\t\t\twarning_errno(\"unable to remove '%s'\", path.buf);\n+\t\t\t\tstrbuf_release(&path);\n+\t\t\t}\n+\t\t\tfree(data->destination_target);\n+\t\t\tfree(data);\n+\t\t\ttransaction->backend_data = NULL;\n+\t\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\t\t\treturn special_ret;\n+\t\t}\n+\t}\n \n \tif (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n \t\treturn files_transaction_finish_initial(refs, transaction, err);\n@@ -3476,11 +3619,105 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \n static int files_transaction_abort(struct ref_store *ref_store,\n \t\t\t\t   struct ref_transaction *transaction,\n-\t\t\t\t   struct strbuf *err UNUSED)\n+\t\t\t\t   struct strbuf *err)\n {\n \tstruct files_ref_store *refs =\n \t\tfiles_downcast(ref_store, 0, \"ref_transaction_abort\");\n \n+\t{\n+\t\tstruct ref_update *update =\n+\t\t\tref_transaction_copy_or_rename_update(transaction);\n+\n+\t\tif (update) {\n+\t\t\tstruct ref_copy_or_rename_update *operation =\n+\t\t\t\tupdate->copy_or_rename;\n+\t\t\tstruct files_copy_or_rename_transaction_data *data =\n+\t\t\t\ttransaction->backend_data;\n+\t\t\tstruct strbuf new_log = STRBUF_INIT;\n+\t\t\tstruct strbuf destination_log = STRBUF_INIT;\n+\t\t\tstruct strbuf temporary_log = STRBUF_INIT;\n+\t\t\tstruct ref_transaction *restore_transaction = NULL;\n+\t\t\tstruct ref_lock *lock;\n+\t\t\tint ret = 0;\n+\n+\t\t\tif (data->lock)\n+\t\t\t\tunlock_ref(data->lock);\n+\t\t\tif (operation->type == REF_UPDATE_RENAME) {\n+\t\t\t\tlock = lock_ref_oid_basic(refs, operation->old_refname, err);\n+\t\t\t\tif (!lock ||\n+\t\t\t\t    write_ref_to_lockfile(refs, lock, &data->orig_oid, err) ||\n+\t\t\t\t    commit_ref_update(refs, lock, &data->orig_oid, NULL,\n+\t\t\t\t\t\t      REF_SKIP_CREATE_REFLOG, err))\n+\t\t\t\t\tret = -1;\n+\t\t\t}\n+\n+\t\t\tif (data->logmoved) {\n+\t\t\t\tfiles_reflog_path(refs, &new_log, update->refname);\n+\t\t\t\tif (operation->type == REF_UPDATE_RENAME) {\n+\t\t\t\t\tfiles_reflog_path(refs, &temporary_log, TMP_RENAMED_LOG);\n+\t\t\t\t\tif (rename(new_log.buf, temporary_log.buf) < 0) {\n+\t\t\t\t\t\tstrbuf_addf(err, \"unable to restore logfile %s: %s\",\n+\t\t\t\t\t\t\t    operation->old_refname, strerror(errno));\n+\t\t\t\t\t\tret = -1;\n+\t\t\t\t\t} else {\n+\t\t\t\t\t\ttry_remove_empty_parents(refs,\n+\t\t\t\t\t\t\t\t\t update->refname,\n+\t\t\t\t\t\t\t\t\t REMOVE_EMPTY_PARENTS_REFLOG);\n+\t\t\t\t\t\tif (rename_tmp_log(refs,\n+\t\t\t\t\t\t\t\t   operation->old_refname)) {\n+\t\t\t\t\t\t\tstrbuf_addf(err, \"unable to restore logfile %s: %s\",\n+\t\t\t\t\t\t\t\t    operation->old_refname,\n+\t\t\t\t\t\t\t\t    strerror(errno));\n+\t\t\t\t\t\t\tret = -1;\n+\t\t\t\t\t\t}\n+\t\t\t\t\t}\n+\t\t\t\t} else if (unlink(new_log.buf) < 0 && errno != ENOENT) {\n+\t\t\t\t\tstrbuf_addf(err, \"unable to remove logfile %s: %s\",\n+\t\t\t\t\t\t    update->refname, strerror(errno));\n+\t\t\t\t\tret = -1;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t\tif (data->destination_log_backed_up) {\n+\t\t\t\tfiles_reflog_path(refs, &destination_log,\n+\t\t\t\t\t\t  TMP_RENAMED_LOG_DESTINATION);\n+\t\t\t\tif (rename(destination_log.buf, new_log.buf) < 0) {\n+\t\t\t\t\tstrbuf_addf(err, \"unable to restore logfile %s: %s\",\n+\t\t\t\t\t\t    update->refname, strerror(errno));\n+\t\t\t\t\tret = -1;\n+\t\t\t\t}\n+\t\t\t}\n+\n+\t\t\tif (operation->type == REF_UPDATE_RENAME &&\n+\t\t\t    data->destination_exists) {\n+\t\t\t\trestore_transaction = ref_store_transaction_begin(\n+\t\t\t\t\t&refs->base, REF_TRANSACTION_FLAG_SKIP_HOOK, err);\n+\t\t\t\tif (!restore_transaction ||\n+\t\t\t\t    ref_transaction_update(restore_transaction,\n+\t\t\t\t\t\t\t   update->refname,\n+\t\t\t\t\t\t\t   data->destination_target ? NULL :\n+\t\t\t\t\t\t\t\t\t\t      &data->destination_oid,\n+\t\t\t\t\t\t\t   NULL,\n+\t\t\t\t\t\t\t   data->destination_target,\n+\t\t\t\t\t\t\t   NULL,\n+\t\t\t\t\t\t\t   REF_NO_DEREF |\n+\t\t\t\t\t\t\t\t   REF_SKIP_CREATE_REFLOG,\n+\t\t\t\t\t\t\t   NULL, err) ||\n+\t\t\t\t    ref_transaction_commit(restore_transaction, err))\n+\t\t\t\t\tret = -1;\n+\t\t\t\tref_transaction_free(restore_transaction);\n+\t\t\t}\n+\n+\t\t\tstrbuf_release(&destination_log);\n+\t\t\tstrbuf_release(&temporary_log);\n+\t\t\tstrbuf_release(&new_log);\n+\t\t\tfree(data->destination_target);\n+\t\t\tfree(data);\n+\t\t\ttransaction->backend_data = NULL;\n+\t\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\t\t\treturn ret;\n+\t\t}\n+\t}\n+\n \tfiles_transaction_cleanup(refs, transaction);\n \treturn 0;\n }\n@@ -4095,8 +4332,6 @@ struct ref_storage_be refs_be_files = {\n \n \t.optimize = files_optimize,\n \t.optimize_required = files_optimize_required,\n-\t.rename_ref = files_rename_ref,\n-\t.copy_ref = files_copy_ref,\n \n \t.iterator_begin = files_ref_iterator_begin,\n \t.read_raw_ref = files_read_raw_ref,\ndiff --git a/refs/packed-backend.c b/refs/packed-backend.c\nindex a73fc6aca..364a91291 100644\n--- a/refs/packed-backend.c\n+++ b/refs/packed-backend.c\n@@ -2164,8 +2164,6 @@ struct ref_storage_be refs_be_packed = {\n \t.optimize = packed_optimize,\n \t.optimize_required = packed_optimize_required,\n \n-\t.rename_ref = NULL,\n-\t.copy_ref = NULL,\n \n \t.iterator_begin = packed_ref_iterator_begin,\n \t.read_raw_ref = packed_read_raw_ref,\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex c3ac7b556..5d4dc0171 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -155,9 +155,33 @@ struct ref_update {\n \t */\n \tstruct ref_update *parent_update;\n \n+\t/*\n+\t * Copy and rename operations require backend-specific handling while\n+\t * still exposing their logical updates to transaction hooks. Keep that\n+\t * state on the destination update so it composes with other updates in\n+\t * the transaction instead of making copy or rename a transaction-wide\n+\t * property.\n+\t */\n+\tstruct ref_copy_or_rename_update *copy_or_rename;\n+\n \tconst char refname[FLEX_ARRAY];\n };\n \n+enum ref_copy_or_rename_type {\n+\tREF_UPDATE_RENAME,\n+\tREF_UPDATE_COPY,\n+};\n+\n+struct ref_copy_or_rename_update {\n+\tenum ref_copy_or_rename_type type;\n+\tchar *old_refname;\n+\tchar *logmsg;\n+\tstruct object_id source_oid;\n+\tstruct object_id destination_oid;\n+\tchar *destination_target;\n+\tunsigned int destination_exists:1;\n+};\n+\n int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,\n \t\t      struct object_id *oid, struct strbuf *referent,\n \t\t      unsigned int *type, int *failure_errno);\n@@ -187,6 +211,13 @@ struct ref_update *ref_transaction_add_update(\n \t\tconst char *committer_info,\n \t\tconst char *msg);\n \n+int refs_delete_ref_with_transaction_flags(struct ref_store *refs,\n+\t\t\t\t\t   const char *msg,\n+\t\t\t\t\t   const char *refname,\n+\t\t\t\t\t   const struct object_id *old_oid,\n+\t\t\t\t\t   unsigned int flags,\n+\t\t\t\t\t   unsigned int transaction_flags);\n+\n /*\n  * Transaction states.\n  *\n@@ -242,6 +273,12 @@ struct ref_transaction {\n \tuint64_t max_index;\n };\n \n+/* Suppress hooks for a transaction nested inside another refs operation. */\n+#define REF_TRANSACTION_FLAG_SKIP_HOOK (1 << 2)\n+\n+struct ref_update *ref_transaction_copy_or_rename_update(\n+\tstruct ref_transaction *transaction);\n+\n /*\n  * Check for entries in extras that are within the specified\n  * directory, where dirname is a reference directory name including\n@@ -451,13 +488,6 @@ typedef int optimize_required_fn(struct ref_store *ref_store,\n \t\t\t\t struct refs_optimize_opts *opts,\n \t\t\t\t bool *required);\n \n-typedef int rename_ref_fn(struct ref_store *ref_store,\n-\t\t\t  const char *oldref, const char *newref,\n-\t\t\t  const char *logmsg);\n-typedef int copy_ref_fn(struct ref_store *ref_store,\n-\t\t\t  const char *oldref, const char *newref,\n-\t\t\t  const char *logmsg);\n-\n /*\n  * Iterate over the references in `ref_store` whose names start with\n  * `prefix`. `prefix` is matched as a literal string, without regard\n@@ -577,9 +607,6 @@ struct ref_storage_be {\n \n \toptimize_fn *optimize;\n \toptimize_required_fn *optimize_required;\n-\trename_ref_fn *rename_ref;\n-\tcopy_ref_fn *copy_ref;\n-\n \tref_iterator_begin_fn *iterator_begin;\n \tread_raw_ref_fn *read_raw_ref;\n \ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 10db03991..589fcc998 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -953,6 +953,14 @@ struct reftable_transaction_data {\n \tsize_t args_nr, args_alloc;\n };\n \n+struct reftable_copy_or_rename_transaction_data {\n+\tstruct reftable_addition *addition;\n+};\n+\n+static int reftable_be_copy_or_rename_prepare(struct ref_store *ref_store,\n+\t\t\t\t\t       struct ref_transaction *transaction,\n+\t\t\t\t\t       struct strbuf *err);\n+\n static void free_transaction_data(struct reftable_transaction_data *tx_data)\n {\n \tif (!tx_data)\n@@ -1326,6 +1334,10 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \tsize_t i;\n \tint ret;\n \n+\tif (ref_transaction_copy_or_rename_update(transaction))\n+\t\treturn reftable_be_copy_or_rename_prepare(ref_store, transaction,\n+\t\t\t\t\t\t\t   err);\n+\n \tret = refs->err;\n \tif (ret < 0)\n \t\tgoto done;\n@@ -1419,7 +1431,20 @@ static int reftable_be_transaction_abort(struct ref_store *ref_store UNUSED,\n \t\t\t\t\t struct ref_transaction *transaction,\n \t\t\t\t\t struct strbuf *err UNUSED)\n {\n-\tstruct reftable_transaction_data *tx_data = transaction->backend_data;\n+\tstruct reftable_transaction_data *tx_data;\n+\n+\tif (ref_transaction_copy_or_rename_update(transaction)) {\n+\t\tstruct reftable_copy_or_rename_transaction_data *data =\n+\t\t\ttransaction->backend_data;\n+\n+\t\treftable_addition_destroy(data->addition);\n+\t\tfree(data);\n+\t\ttransaction->backend_data = NULL;\n+\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\t\treturn 0;\n+\t}\n+\n+\ttx_data = transaction->backend_data;\n \tfree_transaction_data(tx_data);\n \ttransaction->state = REF_TRANSACTION_CLOSED;\n \treturn 0;\n@@ -1667,9 +1692,28 @@ static int reftable_be_transaction_finish(struct ref_store *ref_store UNUSED,\n \t\t\t\t\t  struct ref_transaction *transaction,\n \t\t\t\t\t  struct strbuf *err)\n {\n-\tstruct reftable_transaction_data *tx_data = transaction->backend_data;\n+\tstruct reftable_transaction_data *tx_data;\n \tint ret = 0;\n \n+\tif (ref_transaction_copy_or_rename_update(transaction)) {\n+\t\tstruct reftable_copy_or_rename_transaction_data *data =\n+\t\t\ttransaction->backend_data;\n+\t\tint special_ret = reftable_addition_commit(data->addition);\n+\n+\t\treftable_addition_destroy(data->addition);\n+\t\tfree(data);\n+\t\ttransaction->backend_data = NULL;\n+\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\t\tif (special_ret < 0) {\n+\t\t\tstrbuf_addf(err, _(\"reftable: transaction failure: %s\"),\n+\t\t\t\t    reftable_error_str(special_ret));\n+\t\t\treturn -1;\n+\t\t}\n+\t\treturn 0;\n+\t}\n+\n+\ttx_data = transaction->backend_data;\n+\n \tfor (size_t i = 0; i < tx_data->args_nr; i++) {\n \t\ttx_data->args[i].max_index = transaction->max_index;\n \n@@ -1764,17 +1808,20 @@ struct write_create_symref_arg {\n struct write_copy_arg {\n \tstruct reftable_ref_store *refs;\n \tstruct reftable_backend *be;\n+\tstruct strbuf *err;\n \tconst char *oldname;\n \tconst char *newname;\n \tconst char *logmsg;\n \tint delete_old;\n+\tstruct ref_copy_or_rename_update *operation;\n };\n \n static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n {\n \tstruct write_copy_arg *arg = cb_data;\n \tuint64_t deletion_ts, creation_ts;\n-\tstruct reftable_ref_record old_ref = {0}, refs[2] = {0};\n+\tstruct reftable_ref_record old_ref = {0}, destination_ref = {0};\n+\tstruct reftable_ref_record refs[2] = {0};\n \tstruct reftable_log_record old_log = {0}, *logs = NULL;\n \tstruct reftable_iterator it = {0};\n \tstruct string_list skip = STRING_LIST_INIT_NODUP;\n@@ -1789,14 +1836,75 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n \t\tBUG(\"failed splitting committer info\");\n \n \tif (reftable_stack_read_ref(arg->be->stack, arg->oldname, &old_ref)) {\n-\t\tret = error(_(\"refname %s not found\"), arg->oldname);\n+\t\tstrbuf_addf(arg->err, _(\"refname %s not found\"), arg->oldname);\n+\t\tret = -1;\n \t\tgoto done;\n \t}\n \tif (old_ref.value_type == REFTABLE_REF_SYMREF) {\n-\t\tret = error(_(\"refname %s is a symbolic ref, copying it is not supported\"),\n+\t\tstrbuf_addf(arg->err,\n+\t\t\t    _(\"refname %s is a symbolic ref, copying it is not supported\"),\n \t\t\t    arg->oldname);\n+\t\tret = -1;\n \t\tgoto done;\n \t}\n+\tif (arg->operation) {\n+\t\tstruct object_id oid;\n+\n+\t\tif (old_ref.value_type == REFTABLE_REF_VAL2)\n+\t\t\toidread(&oid, old_ref.value.val2.value,\n+\t\t\t\targ->refs->base.repo->hash_algo);\n+\t\telse\n+\t\t\toidread(&oid, old_ref.value.val1,\n+\t\t\t\targ->refs->base.repo->hash_algo);\n+\t\tif (!oideq(&oid, &arg->operation->source_oid)) {\n+\t\t\tstrbuf_addf(arg->err,\n+\t\t\t\t    _(\"refname %s is at %s but expected %s\"),\n+\t\t\t\t    arg->oldname, oid_to_hex(&oid),\n+\t\t\t\t    oid_to_hex(&arg->operation->source_oid));\n+\t\t\tret = -1;\n+\t\t\tgoto done;\n+\t\t}\n+\n+\t\tret = reftable_stack_read_ref(arg->be->stack, arg->newname,\n+\t\t\t\t\t      &destination_ref);\n+\t\tif (ret < 0)\n+\t\t\tgoto done;\n+\t\tif (arg->operation->destination_exists != !ret) {\n+\t\t\tstrbuf_addf(arg->err,\n+\t\t\t\t    _(\"refname %s changed while renaming\"),\n+\t\t\t\t    arg->newname);\n+\t\t\tret = -1;\n+\t\t\tgoto done;\n+\t\t}\n+\t\tif (!ret) {\n+\t\t\tif (destination_ref.value_type == REFTABLE_REF_SYMREF) {\n+\t\t\t\tif (!arg->operation->destination_target ||\n+\t\t\t\t    strcmp(destination_ref.value.symref,\n+\t\t\t\t\t   arg->operation->destination_target)) {\n+\t\t\t\t\tstrbuf_addf(arg->err,\n+\t\t\t\t\t\t    _(\"refname %s changed while renaming\"),\n+\t\t\t\t\t\t    arg->newname);\n+\t\t\t\t\tret = -1;\n+\t\t\t\t\tgoto done;\n+\t\t\t\t}\n+\t\t\t} else {\n+\t\t\t\tif (destination_ref.value_type == REFTABLE_REF_VAL2)\n+\t\t\t\t\toidread(&oid, destination_ref.value.val2.value,\n+\t\t\t\t\t\targ->refs->base.repo->hash_algo);\n+\t\t\t\telse\n+\t\t\t\t\toidread(&oid, destination_ref.value.val1,\n+\t\t\t\t\t\targ->refs->base.repo->hash_algo);\n+\t\t\t\tif (arg->operation->destination_target ||\n+\t\t\t\t    !oideq(&oid, &arg->operation->destination_oid)) {\n+\t\t\t\t\tstrbuf_addf(arg->err,\n+\t\t\t\t\t\t    _(\"refname %s changed while renaming\"),\n+\t\t\t\t\t\t    arg->newname);\n+\t\t\t\t\tret = -1;\n+\t\t\t\t\tgoto done;\n+\t\t\t\t}\n+\t\t\t}\n+\t\t}\n+\t}\n \n \t/*\n \t * There's nothing to do in case the old and new name are the same, so\n@@ -1815,7 +1923,7 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n \tret = refs_verify_refname_available(&arg->refs->base, arg->newname,\n \t\t\t\t\t    NULL, &skip, 0, &errbuf);\n \tif (ret < 0) {\n-\t\terror(\"%s\", errbuf.buf);\n+\t\tstrbuf_addbuf(arg->err, &errbuf);\n \t\tgoto done;\n \t}\n \n@@ -1980,68 +2088,63 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n \tfor (i = 0; i < ARRAY_SIZE(refs); i++)\n \t\treftable_ref_record_release(&refs[i]);\n \treftable_ref_record_release(&old_ref);\n+\treftable_ref_record_release(&destination_ref);\n \treftable_log_record_release(&old_log);\n \treturn ret;\n }\n \n-static int reftable_be_rename_ref(struct ref_store *ref_store,\n-\t\t\t\t  const char *oldrefname,\n-\t\t\t\t  const char *newrefname,\n-\t\t\t\t  const char *logmsg)\n+static int reftable_be_copy_or_rename_prepare(struct ref_store *ref_store,\n+\t\t\t\t\t       struct ref_transaction *transaction,\n+\t\t\t\t\t       struct strbuf *err)\n {\n \tstruct reftable_ref_store *refs =\n-\t\treftable_be_downcast(ref_store, REF_STORE_WRITE, \"rename_ref\");\n+\t\treftable_be_downcast(ref_store, REF_STORE_WRITE,\n+\t\t\t\t     \"ref_transaction_prepare\");\n+\tstruct reftable_copy_or_rename_transaction_data *data = NULL;\n+\tstruct ref_update *update =\n+\t\tref_transaction_copy_or_rename_update(transaction);\n+\tstruct ref_copy_or_rename_update *operation = update->copy_or_rename;\n \tstruct write_copy_arg arg = {\n \t\t.refs = refs,\n-\t\t.oldname = oldrefname,\n-\t\t.newname = newrefname,\n-\t\t.logmsg = logmsg,\n-\t\t.delete_old = 1,\n+\t\t.err = err,\n+\t\t.oldname = operation->old_refname,\n+\t\t.newname = update->refname,\n+\t\t.logmsg = operation->logmsg,\n+\t\t.delete_old = operation->type == REF_UPDATE_RENAME,\n+\t\t.operation = operation,\n \t};\n \tint ret;\n \n+\tCALLOC_ARRAY(data, 1);\n \tret = refs->err;\n \tif (ret < 0)\n \t\tgoto done;\n-\n-\tret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);\n+\tret = backend_for(&arg.be, refs, update->refname,\n+\t\t\t  &arg.newname, 1);\n \tif (ret)\n \t\tgoto done;\n-\tret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,\n-\t\t\t\t &reftable_be_write_options(refs)->opts);\n-\n-done:\n-\tassert(ret != REFTABLE_API_ERROR);\n-\treturn ret;\n-}\n-\n-static int reftable_be_copy_ref(struct ref_store *ref_store,\n-\t\t\t\tconst char *oldrefname,\n-\t\t\t\tconst char *newrefname,\n-\t\t\t\tconst char *logmsg)\n-{\n-\tstruct reftable_ref_store *refs =\n-\t\treftable_be_downcast(ref_store, REF_STORE_WRITE, \"copy_ref\");\n-\tstruct write_copy_arg arg = {\n-\t\t.refs = refs,\n-\t\t.oldname = oldrefname,\n-\t\t.newname = newrefname,\n-\t\t.logmsg = logmsg,\n-\t};\n-\tint ret;\n-\n-\tret = refs->err;\n-\tif (ret < 0)\n+\tret = reftable_stack_addition_new(&data->addition, arg.be->stack,\n+\t\t\t\t\t  &reftable_be_write_options(refs)->opts);\n+\tif (ret)\n \t\tgoto done;\n-\n-\tret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);\n+\tret = reftable_addition_add(data->addition, &write_copy_table, &arg);\n \tif (ret)\n \t\tgoto done;\n-\tret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,\n-\t\t\t\t &reftable_be_write_options(refs)->opts);\n+\n+\ttransaction->backend_data = data;\n+\ttransaction->state = REF_TRANSACTION_PREPARED;\n+\treturn 0;\n \n done:\n \tassert(ret != REFTABLE_API_ERROR);\n+\tif (data) {\n+\t\treftable_addition_destroy(data->addition);\n+\t\tfree(data);\n+\t}\n+\ttransaction->state = REF_TRANSACTION_CLOSED;\n+\tif (ret && !err->len)\n+\t\tstrbuf_addf(err, _(\"reftable: transaction prepare: %s\"),\n+\t\t\t    reftable_error_str(ret));\n \treturn ret;\n }\n \n@@ -2872,8 +2975,6 @@ struct ref_storage_be refs_be_reftable = {\n \t.optimize = reftable_be_optimize,\n \t.optimize_required = reftable_be_optimize_required,\n \n-\t.rename_ref = reftable_be_rename_ref,\n-\t.copy_ref = reftable_be_copy_ref,\n \n \t.iterator_begin = reftable_be_iterator_begin,\n \t.read_raw_ref = reftable_be_read_raw_ref,\ndiff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh\nindex 4fe9d9b23..116b2ff07 100755\n--- a/t/t1416-ref-transaction-hooks.sh\n+++ b/t/t1416-ref-transaction-hooks.sh\n@@ -93,6 +93,148 @@ test_expect_success 'hook gets all queued updates in committed state' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'hook gets both updates when renaming a branch' '\n+\ttest_when_finished \"rm -f actual\" &&\n+\tgit branch old PRE &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\techo \"$1\" >>actual &&\n+\t\tcat >>actual\n+\tEOF\n+\tcat >expect <<-EOF &&\n+\tpreparing\n+\t$PRE_OID $ZERO_OID refs/heads/old\n+\t$ZERO_OID $PRE_OID refs/heads/new\n+\tprepared\n+\t$PRE_OID $ZERO_OID refs/heads/old\n+\t$ZERO_OID $PRE_OID refs/heads/new\n+\tcommitted\n+\t$PRE_OID $ZERO_OID refs/heads/old\n+\t$ZERO_OID $PRE_OID refs/heads/new\n+\tEOF\n+\tgit branch -m old new &&\n+\ttest_cmp expect actual &&\n+\ttest_must_fail git rev-parse --verify refs/heads/old &&\n+\ttest_cmp_rev PRE refs/heads/new\n+'\n+\n+test_expect_success 'hook gets destination update when copying a branch' '\n+\ttest_when_finished \"rm -f actual\" &&\n+\tgit branch copy-source PRE &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\techo \"$1\" >>actual &&\n+\t\tcat >>actual\n+\tEOF\n+\tcat >expect <<-EOF &&\n+\tpreparing\n+\t$ZERO_OID $PRE_OID refs/heads/copy-destination\n+\tprepared\n+\t$ZERO_OID $PRE_OID refs/heads/copy-destination\n+\tcommitted\n+\t$ZERO_OID $PRE_OID refs/heads/copy-destination\n+\tEOF\n+\tgit branch -c copy-source copy-destination &&\n+\ttest_cmp expect actual &&\n+\ttest_cmp_rev PRE refs/heads/copy-source &&\n+\ttest_cmp_rev PRE refs/heads/copy-destination\n+'\n+\n+test_expect_success 'hook gets overwritten values for forced rename and copy' '\n+\tgit branch force-old PRE &&\n+\tgit branch force-new POST &&\n+\tgit branch force-copy-source PRE &&\n+\tgit branch force-copy-destination POST &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\tif test \"$1\" = committed\n+\t\tthen\n+\t\t\tcat >>actual\n+\t\tfi\n+\tEOF\n+\tgit branch -M force-old force-new &&\n+\tgit branch -C force-copy-source force-copy-destination &&\n+\tcat >expect <<-EOF &&\n+\t$PRE_OID $ZERO_OID refs/heads/force-old\n+\t$POST_OID $PRE_OID refs/heads/force-new\n+\t$POST_OID $PRE_OID refs/heads/force-copy-destination\n+\tEOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'hook can abort a branch rename after preparation' '\n+\tgit branch abort-old PRE &&\n+\tgit branch abort-new POST &&\n+\tgit reflog show --format=%gs abort-old >old-log &&\n+\tgit reflog show --format=%gs abort-new >new-log &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\ttest_must_fail git branch -M abort-old abort-new &&\n+\ttest_cmp_rev PRE refs/heads/abort-old &&\n+\ttest_cmp_rev POST refs/heads/abort-new &&\n+\tgit reflog show --format=%gs abort-old >old-log-after &&\n+\tgit reflog show --format=%gs abort-new >new-log-after &&\n+\ttest_cmp old-log old-log-after &&\n+\ttest_cmp new-log new-log-after\n+'\n+\n+test_expect_success 'hook can abort a D/F branch rename after preparation' '\n+\tgit branch df-old PRE &&\n+\tgit reflog show --format=%gs df-old >df-log &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\ttest_must_fail git branch -m df-old df-old/child &&\n+\ttest_cmp_rev PRE refs/heads/df-old &&\n+\ttest_must_fail git rev-parse --verify refs/heads/df-old/child &&\n+\tgit reflog show --format=%gs df-old >df-log-after &&\n+\ttest_cmp df-log df-log-after\n+'\n+\n+test_expect_success 'hook can abort a reverse D/F rename after preparation' '\n+\tgit branch reverse/old PRE &&\n+\tgit reflog show --format=%gs reverse/old >reverse-log &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\ttest_must_fail git branch -m reverse/old reverse &&\n+\ttest_cmp_rev PRE refs/heads/reverse/old &&\n+\ttest_must_fail git rev-parse --verify refs/heads/reverse &&\n+\tgit reflog show --format=%gs reverse/old >reverse-log-after &&\n+\ttest_cmp reverse-log reverse-log-after\n+'\n+\n+test_expect_success 'hook can abort a forced branch copy after preparation' '\n+\tgit branch copy-abort-old PRE &&\n+\tgit branch copy-abort-new POST &&\n+\tgit reflog show --format=%gs copy-abort-old >copy-old-log &&\n+\tgit reflog show --format=%gs copy-abort-new >copy-new-log &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\ttest_must_fail git branch -C copy-abort-old copy-abort-new &&\n+\ttest_cmp_rev PRE refs/heads/copy-abort-old &&\n+\ttest_cmp_rev POST refs/heads/copy-abort-new &&\n+\tgit reflog show --format=%gs copy-abort-old >copy-old-log-after &&\n+\tgit reflog show --format=%gs copy-abort-new >copy-new-log-after &&\n+\ttest_cmp copy-old-log copy-old-log-after &&\n+\ttest_cmp copy-new-log copy-new-log-after\n+'\n+\n+test_expect_success 'branch rename detects an update during preparing hook' '\n+\tgit branch race-old PRE &&\n+\tgit branch race-new POST &&\n+\ttest_hook reference-transaction <<-\\EOF &&\n+\t\tmarker=$(git rev-parse --git-path rename-race-once)\n+\t\tif test \"$1\" = preparing && test ! -e \"$marker\"\n+\t\tthen\n+\t\t\t>\"$marker\" &&\n+\t\t\tgit update-ref refs/heads/race-old POST\n+\t\tfi\n+\tEOF\n+\ttest_must_fail git branch -M race-old race-new &&\n+\ttest_cmp_rev POST refs/heads/race-old &&\n+\ttest_cmp_rev POST refs/heads/race-new\n+'\n+\n test_expect_success 'hook gets all queued updates in aborted state' '\n \ttest_when_finished \"rm actual\" &&\n \tgit reset --hard PRE &&\n-- \n2.39.3 (Apple Git-146)\n"},{"id":"553667","messageId":"CACQ=SRG5ajG-+vHT8TPDif2Y0C9JFaXFuUesyPFHbDpJCq-76Q@mail.gmail.com","threadId":"66350","inReplyTo":"20260923133651.74120-1-maciej.ciemborowicz@gmail.com","subject":"Re: [PATCH v2] refs: run copy and rename through transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-09-30T03:32:53Z","receivedAt":"2026-09-30T03:33:07Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"I'd appreciate a code review of v2.\n\nThanks,\n- Maciej Ciemborowicz\n\nOn Wed, Sep 23, 2026 at 3:36 PM Maciej Ciemborowicz\n<maciej.ciemborowicz@gmail.com> wrote:\n>\n> Reference copy and rename operations bypass the transaction API.\n> Consequently, the reference-transaction hook sees only the source deletion\n> with the files backend and no useful update with the reftable backend.\n>\n> Represent both operations as reference transactions containing their\n> logical updates. A rename is a deletion of the old reference and creation\n> of the new reference in the same transaction. Attach operation-specific\n> state to the destination update instead of making copy or rename a property\n> of the entire transaction.\n>\n> Retain backend-specific reflog handling: the files backend stages its\n> existing rename procedure across prepare, finish and abort, while reftable\n> stages an addition while holding the stack lock. Suppress hooks for the\n> files backend's nested deletion transactions so that callers observe one\n> logical transaction.\n>\n> Record and verify the source and destination values after taking backend\n> locks. This rejects concurrent changes instead of applying a rename or copy\n> that differs from the payload shown to the preparing hook. Preserve D/F\n> renames and restore overwritten references and reflogs when a prepared hook\n> rejects the operation.\n>\n> Add tests covering rename, copy, forced updates, both directions of D/F\n> conflicts, concurrent updates and prepared-hook rollback.\n>\n> Helped-by: Karthik Nayak <karthik.188@gmail.com>\n> Signed-off-by: Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com>\n> ---\n> Apologies for the unrelated Subject header on my earlier reply. This\n> reroll incorporates the points discussed there.\n>\n> Changes since v1:\n>\n>  * Keep copy/rename state on the destination ref update instead of the\n>    generic transaction, so the operation is no longer a transaction-wide\n>    property.\n>  * Keep REF_TRANSACTION_FLAG_SKIP_HOOK private to the refs implementation.\n>  * Use bool for the two-state copy parameter.\n>  * Apply Junio's commit-message wording suggestions.\n>\n> Range-diff against v1:\n> 1:  de0a5a9f7 ! 1:  d852537d8 refs: run copy and rename through transactions\n>     @@ Metadata\n>       ## Commit message ##\n>          refs: run copy and rename through transactions\n>\n>     -    Reference copy and rename operations currently bypass the transaction API.\n>     +    Reference copy and rename operations bypass the transaction API.\n>          Consequently, the reference-transaction hook sees only the source deletion\n>          with the files backend and no useful update with the reftable backend.\n>\n>          Represent both operations as reference transactions containing their\n>          logical updates. A rename is a deletion of the old reference and creation\n>     -    of the new reference in the same transaction. Retain backend-specific\n>     -    reflog handling: the files backend stages its existing rename procedure\n>     -    across prepare, finish and abort, while reftable stages an addition while\n>     -    holding the stack lock. Suppress hooks for the files backend's nested\n>     -    deletion transactions so that callers observe one logical transaction.\n>     +    of the new reference in the same transaction. Attach operation-specific\n>     +    state to the destination update instead of making copy or rename a property\n>     +    of the entire transaction.\n>     +\n>     +    Retain backend-specific reflog handling: the files backend stages its\n>     +    existing rename procedure across prepare, finish and abort, while reftable\n>     +    stages an addition while holding the stack lock. Suppress hooks for the\n>     +    files backend's nested deletion transactions so that callers observe one\n>     +    logical transaction.\n>\n>          Record and verify the source and destination values after taking backend\n>          locks. This rejects concurrent changes instead of applying a rename or copy\n>     @@ Commit message\n>          renames and restore overwritten references and reflogs when a prepared hook\n>          rejects the operation.\n>\n>     -    Add coverage for rename, copy, forced updates, both directions of D/F\n>     +    Add tests covering rename, copy, forced updates, both directions of D/F\n>          conflicts, concurrent updates and prepared-hook rollback.\n>\n>          Helped-by: Karthik Nayak <karthik.188@gmail.com>\n>     @@ refs.c: int refs_delete_ref(struct ref_store *refs, const char *msg,\n>       {\n>         char c;\n>      @@ refs.c: void ref_transaction_free(struct ref_transaction *transaction)\n>     +   }\n>     +\n>     +   for (i = 0; i < transaction->nr; i++) {\n>     ++          struct ref_copy_or_rename_update *operation =\n>     ++                  transaction->updates[i]->copy_or_rename;\n>     ++\n>     +           free(transaction->updates[i]->msg);\n>     +           free(transaction->updates[i]->committer_info);\n>     +           free((char *)transaction->updates[i]->new_target);\n>     +           free((char *)transaction->updates[i]->old_target);\n>     +           free((char *)transaction->updates[i]->rejection_details);\n>     ++          if (operation) {\n>     ++                  free(operation->old_refname);\n>     ++                  free(operation->logmsg);\n>     ++                  free(operation->destination_target);\n>     ++                  free(operation);\n>     ++          }\n>     +           free(transaction->updates[i]);\n>     +   }\n>\n>     -   string_list_clear(&transaction->refnames, 0);\n>     -   free(transaction->updates);\n>     -+  free(transaction->old_refname);\n>     -+  free(transaction->new_refname);\n>     -+  free(transaction->logmsg);\n>     -+  free(transaction->destination_target);\n>     +@@ refs.c: void ref_transaction_free(struct ref_transaction *transaction)\n>         free(transaction);\n>       }\n>\n>     ++struct ref_update *ref_transaction_copy_or_rename_update(\n>     ++  struct ref_transaction *transaction)\n>     ++{\n>     ++  struct ref_update *operation = NULL;\n>     ++  size_t i;\n>     ++\n>     ++  for (i = 0; i < transaction->nr; i++) {\n>     ++          if (!transaction->updates[i]->copy_or_rename)\n>     ++                  continue;\n>     ++          if (operation)\n>     ++                  BUG(\"multiple copy or rename updates in one transaction\");\n>     ++          operation = transaction->updates[i];\n>     ++  }\n>     ++\n>     ++  return operation;\n>     ++}\n>     ++\n>     + int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,\n>     +                                  size_t update_idx,\n>     +                                  enum ref_transaction_error err,\n>      @@ refs.c: int ref_transaction_prepare(struct ref_transaction *transaction,\n>                 return REF_TRANSACTION_ERROR_GENERIC;\n>\n>     @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n>      -              const char *newref, const char *logmsg)\n>      +static int refs_copy_or_rename_ref(struct ref_store *refs, const char *oldref,\n>      +                             const char *newref, const char *logmsg,\n>     -+                             int copy)\n>     ++                             bool copy)\n>       {\n>      -  char *msg;\n>      -  int retval;\n>      +  struct ref_transaction *transaction = NULL;\n>     ++  struct ref_copy_or_rename_update *operation = NULL;\n>     ++  struct ref_update *destination_update;\n>      +  struct object_id old_oid, new_oid;\n>      +  struct strbuf new_target = STRBUF_INIT;\n>      +  struct strbuf err = STRBUF_INIT;\n>     @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n>      +  transaction = ref_store_transaction_begin(refs, 0, &err);\n>      +  if (!transaction)\n>      +          goto error;\n>     -+  transaction->type = copy ? REF_TRANSACTION_TYPE_COPY :\n>     -+          REF_TRANSACTION_TYPE_RENAME;\n>     -+  transaction->old_refname = xstrdup(oldref);\n>     -+  transaction->new_refname = xstrdup(newref);\n>     -+  transaction->logmsg = xstrdup(msg);\n>     -+  oidcpy(&transaction->source_oid, &old_oid);\n>     -+\n>      +  if (!copy && ref_transaction_delete(transaction, oldref, &old_oid, NULL,\n>      +                                      REF_NO_DEREF, msg, &err))\n>      +          goto error;\n>     @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n>      +  } else {\n>      +          oidclr(&new_oid, refs->repo->hash_algo);\n>      +  }\n>     -+  transaction->destination_exists = new_exists;\n>     -+  if (new_flags & REF_ISSYMREF)\n>     -+          transaction->destination_target = xstrdup(new_target.buf);\n>     -+  else if (transaction->destination_exists)\n>     -+          oidcpy(&transaction->destination_oid, &new_oid);\n>     -+\n>      +  if (ref_transaction_update(transaction, newref, &old_oid,\n>      +                             (new_flags & REF_ISSYMREF) ? NULL : &new_oid,\n>      +                             NULL,\n>     @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n>      +                             NULL, &err))\n>      +          goto error;\n>      +\n>     ++  destination_update = transaction->updates[transaction->nr - 1];\n>     ++  CALLOC_ARRAY(operation, 1);\n>     ++  operation->type = copy ? REF_UPDATE_COPY : REF_UPDATE_RENAME;\n>     ++  operation->old_refname = xstrdup(oldref);\n>     ++  operation->logmsg = xstrdup(msg);\n>     ++  oidcpy(&operation->source_oid, &old_oid);\n>     ++  operation->destination_exists = new_exists;\n>     ++  if (new_flags & REF_ISSYMREF)\n>     ++          operation->destination_target = xstrdup(new_target.buf);\n>     ++  else if (operation->destination_exists)\n>     ++          oidcpy(&operation->destination_oid, &new_oid);\n>     ++  destination_update->copy_or_rename = operation;\n>     ++\n>      +  if (ref_transaction_commit(transaction, &err))\n>      +          goto error;\n>      +\n>     @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n>\n>       const char *ref_update_original_update_refname(struct ref_update *update)\n>\n>     - ## refs.h ##\n>     -@@ refs.h: enum ref_transaction_flag {\n>     -    * while rejecting updates which do not match the expected state.\n>     -    */\n>     -   REF_TRANSACTION_ALLOW_FAILURE = (1 << 1),\n>     -+\n>     -+  /* Suppress hooks for an update nested in another transaction. */\n>     -+  REF_TRANSACTION_FLAG_SKIP_HOOK = (1 << 2),\n>     - };\n>     -\n>     - /*\n>     -\n>       ## refs/debug.c ##\n>      @@ refs/debug.c: static int debug_optimize_required(struct ref_store *ref_store,\n>         return res;\n>     @@ refs/files-backend.c: static int refs_rename_ref_available(struct ref_store *ref\n>      +};\n>      +\n>       static int files_copy_or_rename_ref(struct ref_store *ref_store,\n>     -                       const char *oldrefname, const char *newrefname,\n>     +-                      const char *oldrefname, const char *newrefname,\n>      -                      const char *logmsg, int copy)\n>     -+                      const char *logmsg, int copy,\n>     -+                      struct ref_transaction *transaction)\n>     ++                              struct ref_update *update,\n>     ++                              struct ref_transaction *transaction)\n>       {\n>         struct files_ref_store *refs =\n>      -          files_downcast(ref_store, REF_STORE_WRITE, \"rename_ref\");\n>      +          files_downcast(ref_store, REF_STORE_WRITE,\n>      +                         \"ref_transaction_prepare\");\n>     ++  struct ref_copy_or_rename_update *operation = update->copy_or_rename;\n>     ++  const char *oldrefname = operation->old_refname;\n>     ++  const char *newrefname = update->refname;\n>     ++  const char *logmsg = operation->logmsg;\n>     ++  bool copy = operation->type == REF_UPDATE_COPY;\n>         struct object_id orig_oid;\n>         int flag = 0, logmoved = 0;\n>         struct ref_lock *lock;\n>     @@ refs/files-backend.c: static int files_copy_or_rename_ref(struct ref_store *ref_\n>                                     oldrefname);\n>                 goto out;\n>         }\n>     -+  if (!oideq(&orig_oid, &transaction->source_oid)) {\n>     ++  if (!oideq(&orig_oid, &operation->source_oid)) {\n>      +          ret = error(\"refname %s is at %s but expected %s\",\n>      +                      oldrefname, oid_to_hex(&orig_oid),\n>     -+                      oid_to_hex(&transaction->source_oid));\n>     ++                      oid_to_hex(&operation->source_oid));\n>      +          goto out;\n>      +  }\n>         if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {\n>     @@ refs/files-backend.c: static int files_copy_or_rename_ref(struct ref_store *ref_\n>      +                  goto out;\n>      +          }\n>      +  }\n>     -+  if (destination_exists != transaction->destination_exists) {\n>     ++  if (destination_exists != operation->destination_exists) {\n>      +          ret = error(\"refname %s changed while renaming\", newrefname);\n>      +          goto out;\n>      +  }\n>      +  if (destination_exists) {\n>      +          if (destination_flags & REF_ISSYMREF) {\n>     -+                  if (!transaction->destination_target ||\n>     ++                  if (!operation->destination_target ||\n>      +                      strcmp(destination_target.buf,\n>     -+                             transaction->destination_target)) {\n>     ++                             operation->destination_target)) {\n>      +                          ret = error(\"refname %s changed while renaming\",\n>      +                                      newrefname);\n>      +                          goto out;\n>      +                  }\n>     -+          } else if (transaction->destination_target ||\n>     ++          } else if (operation->destination_target ||\n>      +                     !oideq(&destination_oid,\n>     -+                            &transaction->destination_oid)) {\n>     ++                            &operation->destination_oid)) {\n>      +                  ret = error(\"refname %s changed while renaming\", newrefname);\n>      +                  goto out;\n>      +          }\n>     @@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref\n>         struct ref_transaction *packed_transaction = NULL;\n>\n>         assert(err);\n>     -+  if (transaction->type != REF_TRANSACTION_TYPE_NORMAL)\n>     -+          return files_copy_or_rename_ref(ref_store,\n>     -+                          transaction->old_refname,\n>     -+                          transaction->new_refname,\n>     -+                          transaction->logmsg,\n>     -+                          transaction->type == REF_TRANSACTION_TYPE_COPY,\n>     -+                          transaction);\n>     ++  {\n>     ++          struct ref_update *operation =\n>     ++                  ref_transaction_copy_or_rename_update(transaction);\n>     ++\n>     ++          if (operation)\n>     ++                  return files_copy_or_rename_ref(ref_store, operation,\n>     ++                                                  transaction);\n>     ++  }\n>\n>         if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n>                 goto cleanup;\n>     @@ refs/files-backend.c: static int files_transaction_finish(struct ref_store *ref_\n>\n>\n>         assert(err);\n>     -+  if (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {\n>     -+          struct files_copy_or_rename_transaction_data *data =\n>     -+                  transaction->backend_data;\n>     -+          int special_ret;\n>     ++  {\n>     ++          struct ref_update *update =\n>     ++                  ref_transaction_copy_or_rename_update(transaction);\n>      +\n>     -+          special_ret = commit_ref_update(refs, data->lock, &data->orig_oid,\n>     -+                                          transaction->logmsg, 0, err);\n>     -+          if (special_ret) {\n>     -+                  error(\"unable to write current sha1 into %s: %s\",\n>     -+                        transaction->new_refname, err->buf);\n>     -+                  data->lock = NULL;\n>     -+                  files_transaction_abort(ref_store, transaction, err);\n>     -+                  return special_ret;\n>     -+          } else if (data->destination_log_backed_up) {\n>     -+                  struct strbuf path = STRBUF_INIT;\n>     ++          if (update) {\n>     ++                  struct ref_copy_or_rename_update *operation =\n>     ++                          update->copy_or_rename;\n>     ++                  struct files_copy_or_rename_transaction_data *data =\n>     ++                          transaction->backend_data;\n>     ++                  int special_ret;\n>     ++\n>     ++                  special_ret = commit_ref_update(refs, data->lock, &data->orig_oid,\n>     ++                                                  operation->logmsg, 0, err);\n>     ++                  if (special_ret) {\n>     ++                          error(\"unable to write current sha1 into %s: %s\",\n>     ++                                update->refname, err->buf);\n>     ++                          data->lock = NULL;\n>     ++                          files_transaction_abort(ref_store, transaction, err);\n>     ++                          return special_ret;\n>     ++                  } else if (data->destination_log_backed_up) {\n>     ++                          struct strbuf path = STRBUF_INIT;\n>      +\n>     -+                  files_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);\n>     -+                  if (unlink(path.buf) < 0 && errno != ENOENT)\n>     -+                          warning_errno(\"unable to remove '%s'\", path.buf);\n>     -+                  strbuf_release(&path);\n>     ++                          files_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);\n>     ++                          if (unlink(path.buf) < 0 && errno != ENOENT)\n>     ++                                  warning_errno(\"unable to remove '%s'\", path.buf);\n>     ++                          strbuf_release(&path);\n>     ++                  }\n>     ++                  free(data->destination_target);\n>     ++                  free(data);\n>     ++                  transaction->backend_data = NULL;\n>     ++                  transaction->state = REF_TRANSACTION_CLOSED;\n>     ++                  return special_ret;\n>      +          }\n>     -+          free(data->destination_target);\n>     -+          free(data);\n>     -+          transaction->backend_data = NULL;\n>     -+          transaction->state = REF_TRANSACTION_CLOSED;\n>     -+          return special_ret;\n>      +  }\n>\n>         if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n>     @@ refs/files-backend.c: static int files_transaction_finish(struct ref_store *ref_\n>         struct files_ref_store *refs =\n>                 files_downcast(ref_store, 0, \"ref_transaction_abort\");\n>\n>     -+  if (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {\n>     -+          struct files_copy_or_rename_transaction_data *data =\n>     -+                  transaction->backend_data;\n>     -+          struct strbuf new_log = STRBUF_INIT;\n>     -+          struct strbuf destination_log = STRBUF_INIT;\n>     -+          struct strbuf temporary_log = STRBUF_INIT;\n>     -+          struct ref_transaction *restore_transaction = NULL;\n>     -+          struct ref_lock *lock;\n>     -+          int ret = 0;\n>     ++  {\n>     ++          struct ref_update *update =\n>     ++                  ref_transaction_copy_or_rename_update(transaction);\n>      +\n>     -+          if (data->lock)\n>     -+                  unlock_ref(data->lock);\n>     -+          if (transaction->type == REF_TRANSACTION_TYPE_RENAME) {\n>     -+                  lock = lock_ref_oid_basic(refs, transaction->old_refname, err);\n>     -+                  if (!lock ||\n>     -+                      write_ref_to_lockfile(refs, lock, &data->orig_oid, err) ||\n>     -+                      commit_ref_update(refs, lock, &data->orig_oid, NULL,\n>     -+                                        REF_SKIP_CREATE_REFLOG, err))\n>     -+                          ret = -1;\n>     -+          }\n>     ++          if (update) {\n>     ++                  struct ref_copy_or_rename_update *operation =\n>     ++                          update->copy_or_rename;\n>     ++                  struct files_copy_or_rename_transaction_data *data =\n>     ++                          transaction->backend_data;\n>     ++                  struct strbuf new_log = STRBUF_INIT;\n>     ++                  struct strbuf destination_log = STRBUF_INIT;\n>     ++                  struct strbuf temporary_log = STRBUF_INIT;\n>     ++                  struct ref_transaction *restore_transaction = NULL;\n>     ++                  struct ref_lock *lock;\n>     ++                  int ret = 0;\n>      +\n>     -+          if (data->logmoved) {\n>     -+                  files_reflog_path(refs, &new_log, transaction->new_refname);\n>     -+                  if (transaction->type == REF_TRANSACTION_TYPE_RENAME) {\n>     -+                          files_reflog_path(refs, &temporary_log, TMP_RENAMED_LOG);\n>     -+                          if (rename(new_log.buf, temporary_log.buf) < 0) {\n>     -+                                  strbuf_addf(err, \"unable to restore logfile %s: %s\",\n>     -+                                              transaction->old_refname, strerror(errno));\n>     ++                  if (data->lock)\n>     ++                          unlock_ref(data->lock);\n>     ++                  if (operation->type == REF_UPDATE_RENAME) {\n>     ++                          lock = lock_ref_oid_basic(refs, operation->old_refname, err);\n>     ++                          if (!lock ||\n>     ++                              write_ref_to_lockfile(refs, lock, &data->orig_oid, err) ||\n>     ++                              commit_ref_update(refs, lock, &data->orig_oid, NULL,\n>     ++                                                REF_SKIP_CREATE_REFLOG, err))\n>      +                                  ret = -1;\n>     -+                          } else {\n>     -+                                  try_remove_empty_parents(refs,\n>     -+                                                   transaction->new_refname,\n>     -+                                                   REMOVE_EMPTY_PARENTS_REFLOG);\n>     -+                                  if (rename_tmp_log(refs,\n>     -+                                                     transaction->old_refname)) {\n>     ++                  }\n>     ++\n>     ++                  if (data->logmoved) {\n>     ++                          files_reflog_path(refs, &new_log, update->refname);\n>     ++                          if (operation->type == REF_UPDATE_RENAME) {\n>     ++                                  files_reflog_path(refs, &temporary_log, TMP_RENAMED_LOG);\n>     ++                                  if (rename(new_log.buf, temporary_log.buf) < 0) {\n>      +                                          strbuf_addf(err, \"unable to restore logfile %s: %s\",\n>     -+                                                      transaction->old_refname,\n>     -+                                                      strerror(errno));\n>     ++                                                      operation->old_refname, strerror(errno));\n>      +                                          ret = -1;\n>     ++                                  } else {\n>     ++                                          try_remove_empty_parents(refs,\n>     ++                                                                   update->refname,\n>     ++                                                                   REMOVE_EMPTY_PARENTS_REFLOG);\n>     ++                                          if (rename_tmp_log(refs,\n>     ++                                                             operation->old_refname)) {\n>     ++                                                  strbuf_addf(err, \"unable to restore logfile %s: %s\",\n>     ++                                                              operation->old_refname,\n>     ++                                                              strerror(errno));\n>     ++                                                  ret = -1;\n>     ++                                          }\n>      +                                  }\n>     ++                          } else if (unlink(new_log.buf) < 0 && errno != ENOENT) {\n>     ++                                  strbuf_addf(err, \"unable to remove logfile %s: %s\",\n>     ++                                              update->refname, strerror(errno));\n>     ++                                  ret = -1;\n>      +                          }\n>     -+                  } else if (unlink(new_log.buf) < 0 && errno != ENOENT) {\n>     -+                          strbuf_addf(err, \"unable to remove logfile %s: %s\",\n>     -+                                      transaction->new_refname, strerror(errno));\n>     -+                          ret = -1;\n>      +                  }\n>     -+          }\n>     -+          if (data->destination_log_backed_up) {\n>     -+                  files_reflog_path(refs, &destination_log,\n>     -+                                     TMP_RENAMED_LOG_DESTINATION);\n>     -+                  if (rename(destination_log.buf, new_log.buf) < 0) {\n>     -+                          strbuf_addf(err, \"unable to restore logfile %s: %s\",\n>     -+                                      transaction->new_refname, strerror(errno));\n>     -+                          ret = -1;\n>     ++                  if (data->destination_log_backed_up) {\n>     ++                          files_reflog_path(refs, &destination_log,\n>     ++                                            TMP_RENAMED_LOG_DESTINATION);\n>     ++                          if (rename(destination_log.buf, new_log.buf) < 0) {\n>     ++                                  strbuf_addf(err, \"unable to restore logfile %s: %s\",\n>     ++                                              update->refname, strerror(errno));\n>     ++                                  ret = -1;\n>     ++                          }\n>      +                  }\n>     -+          }\n>      +\n>     -+          if (transaction->type == REF_TRANSACTION_TYPE_RENAME &&\n>     -+              data->destination_exists) {\n>     -+                  restore_transaction = ref_store_transaction_begin(\n>     ++                  if (operation->type == REF_UPDATE_RENAME &&\n>     ++                      data->destination_exists) {\n>     ++                          restore_transaction = ref_store_transaction_begin(\n>      +                                  &refs->base, REF_TRANSACTION_FLAG_SKIP_HOOK, err);\n>     -+                  if (!restore_transaction ||\n>     -+                      ref_transaction_update(restore_transaction,\n>     -+                                             transaction->new_refname,\n>     -+                                             data->destination_target ? NULL :\n>     -+                                                  &data->destination_oid,\n>     -+                                             NULL,\n>     -+                                             data->destination_target,\n>     -+                                             NULL,\n>     -+                                             REF_NO_DEREF |\n>     -+                                                  REF_SKIP_CREATE_REFLOG,\n>     -+                                             NULL, err) ||\n>     -+                      ref_transaction_commit(restore_transaction, err))\n>     -+                          ret = -1;\n>     -+                  ref_transaction_free(restore_transaction);\n>     -+          }\n>     ++                          if (!restore_transaction ||\n>     ++                              ref_transaction_update(restore_transaction,\n>     ++                                                     update->refname,\n>     ++                                                     data->destination_target ? NULL :\n>     ++                                                                                &data->destination_oid,\n>     ++                                                     NULL,\n>     ++                                                     data->destination_target,\n>     ++                                                     NULL,\n>     ++                                                     REF_NO_DEREF |\n>     ++                                                             REF_SKIP_CREATE_REFLOG,\n>     ++                                                     NULL, err) ||\n>     ++                              ref_transaction_commit(restore_transaction, err))\n>     ++                                  ret = -1;\n>     ++                          ref_transaction_free(restore_transaction);\n>     ++                  }\n>      +\n>     -+          strbuf_release(&destination_log);\n>     -+          strbuf_release(&temporary_log);\n>     -+          strbuf_release(&new_log);\n>     -+          free(data->destination_target);\n>     -+          free(data);\n>     -+          transaction->backend_data = NULL;\n>     -+          transaction->state = REF_TRANSACTION_CLOSED;\n>     -+          return ret;\n>     ++                  strbuf_release(&destination_log);\n>     ++                  strbuf_release(&temporary_log);\n>     ++                  strbuf_release(&new_log);\n>     ++                  free(data->destination_target);\n>     ++                  free(data);\n>     ++                  transaction->backend_data = NULL;\n>     ++                  transaction->state = REF_TRANSACTION_CLOSED;\n>     ++                  return ret;\n>     ++          }\n>      +  }\n>      +\n>         files_transaction_cleanup(refs, transaction);\n>     @@ refs/packed-backend.c: struct ref_storage_be refs_be_packed = {\n>         .read_raw_ref = packed_read_raw_ref,\n>\n>       ## refs/refs-internal.h ##\n>     +@@ refs/refs-internal.h: struct ref_update {\n>     +    */\n>     +   struct ref_update *parent_update;\n>     +\n>     ++  /*\n>     ++   * Copy and rename operations require backend-specific handling while\n>     ++   * still exposing their logical updates to transaction hooks. Keep that\n>     ++   * state on the destination update so it composes with other updates in\n>     ++   * the transaction instead of making copy or rename a transaction-wide\n>     ++   * property.\n>     ++   */\n>     ++  struct ref_copy_or_rename_update *copy_or_rename;\n>     ++\n>     +   const char refname[FLEX_ARRAY];\n>     + };\n>     +\n>     ++enum ref_copy_or_rename_type {\n>     ++  REF_UPDATE_RENAME,\n>     ++  REF_UPDATE_COPY,\n>     ++};\n>     ++\n>     ++struct ref_copy_or_rename_update {\n>     ++  enum ref_copy_or_rename_type type;\n>     ++  char *old_refname;\n>     ++  char *logmsg;\n>     ++  struct object_id source_oid;\n>     ++  struct object_id destination_oid;\n>     ++  char *destination_target;\n>     ++  unsigned int destination_exists:1;\n>     ++};\n>     ++\n>     + int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,\n>     +                 struct object_id *oid, struct strbuf *referent,\n>     +                 unsigned int *type, int *failure_errno);\n>      @@ refs/refs-internal.h: struct ref_update *ref_transaction_add_update(\n>                 const char *committer_info,\n>                 const char *msg);\n>     @@ refs/refs-internal.h: struct ref_update *ref_transaction_add_update(\n>       /*\n>        * Transaction states.\n>        *\n>     -@@ refs/refs-internal.h: enum ref_transaction_state {\n>     -   REF_TRANSACTION_CLOSED   = 2\n>     - };\n>     -\n>     -+enum ref_transaction_type {\n>     -+  REF_TRANSACTION_TYPE_NORMAL = 0,\n>     -+  REF_TRANSACTION_TYPE_RENAME,\n>     -+  REF_TRANSACTION_TYPE_COPY,\n>     -+};\n>     -+\n>     - /*\n>     -  * Data structure to hold indices of updates which were rejected, for batched\n>     -  * reference updates. While the updates themselves hold the rejection error,\n>      @@ refs/refs-internal.h: struct ref_transaction {\n>     -   void *backend_data;\n>     -   unsigned int flags;\n>         uint64_t max_index;\n>     -+\n>     -+  /*\n>     -+   * Rename and copy operations need backend-specific reflog handling.\n>     -+   * Their logical updates still live in `updates`, so hooks see the\n>     -+   * operation like any other reference transaction. The fields below\n>     -+   * retain the state that backends verify after taking their locks.\n>     -+   */\n>     -+  enum ref_transaction_type type;\n>     -+  char *old_refname;\n>     -+  char *new_refname;\n>     -+  char *logmsg;\n>     -+  struct object_id source_oid;\n>     -+  struct object_id destination_oid;\n>     -+  char *destination_target;\n>     -+  unsigned int destination_exists:1;\n>       };\n>\n>     ++/* Suppress hooks for a transaction nested inside another refs operation. */\n>     ++#define REF_TRANSACTION_FLAG_SKIP_HOOK (1 << 2)\n>     ++\n>     ++struct ref_update *ref_transaction_copy_or_rename_update(\n>     ++  struct ref_transaction *transaction);\n>     ++\n>       /*\n>     +  * Check for entries in extras that are within the specified\n>     +  * directory, where dirname is a reference directory name including\n>      @@ refs/refs-internal.h: typedef int optimize_required_fn(struct ref_store *ref_store,\n>                                  struct refs_optimize_opts *opts,\n>                                  bool *required);\n>     @@ refs/reftable-backend.c: static int reftable_be_transaction_prepare(struct ref_s\n>         size_t i;\n>         int ret;\n>\n>     -+  if (transaction->type != REF_TRANSACTION_TYPE_NORMAL)\n>     ++  if (ref_transaction_copy_or_rename_update(transaction))\n>      +          return reftable_be_copy_or_rename_prepare(ref_store, transaction,\n>      +                                                     err);\n>      +\n>     @@ refs/reftable-backend.c: static int reftable_be_transaction_abort(struct ref_sto\n>      -  struct reftable_transaction_data *tx_data = transaction->backend_data;\n>      +  struct reftable_transaction_data *tx_data;\n>      +\n>     -+  if (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {\n>     ++  if (ref_transaction_copy_or_rename_update(transaction)) {\n>      +          struct reftable_copy_or_rename_transaction_data *data =\n>      +                  transaction->backend_data;\n>      +\n>     @@ refs/reftable-backend.c: static int reftable_be_transaction_finish(struct ref_st\n>      +  struct reftable_transaction_data *tx_data;\n>         int ret = 0;\n>\n>     -+  if (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {\n>     ++  if (ref_transaction_copy_or_rename_update(transaction)) {\n>      +          struct reftable_copy_or_rename_transaction_data *data =\n>      +                  transaction->backend_data;\n>      +          int special_ret = reftable_addition_commit(data->addition);\n>     @@ refs/reftable-backend.c: struct write_create_symref_arg {\n>         const char *newname;\n>         const char *logmsg;\n>         int delete_old;\n>     -+  struct ref_transaction *transaction;\n>     ++  struct ref_copy_or_rename_update *operation;\n>       };\n>\n>       static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n>     @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n>      +          ret = -1;\n>                 goto done;\n>         }\n>     -+  if (arg->transaction) {\n>     ++  if (arg->operation) {\n>      +          struct object_id oid;\n>      +\n>      +          if (old_ref.value_type == REFTABLE_REF_VAL2)\n>     @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n>      +          else\n>      +                  oidread(&oid, old_ref.value.val1,\n>      +                          arg->refs->base.repo->hash_algo);\n>     -+          if (!oideq(&oid, &arg->transaction->source_oid)) {\n>     ++          if (!oideq(&oid, &arg->operation->source_oid)) {\n>      +                  strbuf_addf(arg->err,\n>      +                              _(\"refname %s is at %s but expected %s\"),\n>      +                              arg->oldname, oid_to_hex(&oid),\n>     -+                              oid_to_hex(&arg->transaction->source_oid));\n>     ++                              oid_to_hex(&arg->operation->source_oid));\n>      +                  ret = -1;\n>      +                  goto done;\n>      +          }\n>     @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n>      +                                        &destination_ref);\n>      +          if (ret < 0)\n>      +                  goto done;\n>     -+          if (arg->transaction->destination_exists != !ret) {\n>     ++          if (arg->operation->destination_exists != !ret) {\n>      +                  strbuf_addf(arg->err,\n>      +                              _(\"refname %s changed while renaming\"),\n>      +                              arg->newname);\n>     @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n>      +          }\n>      +          if (!ret) {\n>      +                  if (destination_ref.value_type == REFTABLE_REF_SYMREF) {\n>     -+                          if (!arg->transaction->destination_target ||\n>     ++                          if (!arg->operation->destination_target ||\n>      +                              strcmp(destination_ref.value.symref,\n>     -+                                     arg->transaction->destination_target)) {\n>     ++                                     arg->operation->destination_target)) {\n>      +                                  strbuf_addf(arg->err,\n>      +                                              _(\"refname %s changed while renaming\"),\n>      +                                              arg->newname);\n>     @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n>      +                          else\n>      +                                  oidread(&oid, destination_ref.value.val1,\n>      +                                          arg->refs->base.repo->hash_algo);\n>     -+                          if (arg->transaction->destination_target ||\n>     -+                              !oideq(&oid, &arg->transaction->destination_oid)) {\n>     ++                          if (arg->operation->destination_target ||\n>     ++                              !oideq(&oid, &arg->operation->destination_oid)) {\n>      +                                  strbuf_addf(arg->err,\n>      +                                              _(\"refname %s changed while renaming\"),\n>      +                                              arg->newname);\n>     @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n>      +          reftable_be_downcast(ref_store, REF_STORE_WRITE,\n>      +                               \"ref_transaction_prepare\");\n>      +  struct reftable_copy_or_rename_transaction_data *data = NULL;\n>     ++  struct ref_update *update =\n>     ++          ref_transaction_copy_or_rename_update(transaction);\n>     ++  struct ref_copy_or_rename_update *operation = update->copy_or_rename;\n>         struct write_copy_arg arg = {\n>                 .refs = refs,\n>      -          .oldname = oldrefname,\n>     @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n>      -          .logmsg = logmsg,\n>      -          .delete_old = 1,\n>      +          .err = err,\n>     -+          .oldname = transaction->old_refname,\n>     -+          .newname = transaction->new_refname,\n>     -+          .logmsg = transaction->logmsg,\n>     -+          .delete_old = transaction->type == REF_TRANSACTION_TYPE_RENAME,\n>     -+          .transaction = transaction,\n>     ++          .oldname = operation->old_refname,\n>     ++          .newname = update->refname,\n>     ++          .logmsg = operation->logmsg,\n>     ++          .delete_old = operation->type == REF_UPDATE_RENAME,\n>     ++          .operation = operation,\n>         };\n>         int ret;\n>\n>     @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri\n>                 goto done;\n>      -\n>      -  ret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);\n>     -+  ret = backend_for(&arg.be, refs, transaction->new_refname,\n>     ++  ret = backend_for(&arg.be, refs, update->refname,\n>      +                    &arg.newname, 1);\n>         if (ret)\n>                 goto done;\n>\n>  refs.c                           | 161 ++++++++++++++---\n>  refs/debug.c                     |  25 ---\n>  refs/files-backend.c             | 297 +++++++++++++++++++++++++++----\n>  refs/packed-backend.c            |   2 -\n>  refs/refs-internal.h             |  47 +++--\n>  refs/reftable-backend.c          | 197 +++++++++++++++-----\n>  t/t1416-ref-transaction-hooks.sh | 142 +++++++++++++++\n>  7 files changed, 732 insertions(+), 139 deletions(-)\n>\n> diff --git a/refs.c b/refs.c\n> index 92d5df5b7..f036ae4b9 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -1004,15 +1004,17 @@ long get_files_ref_lock_timeout_ms(struct repository *repo)\n>         return timeout_ms;\n>  }\n>\n> -int refs_delete_ref(struct ref_store *refs, const char *msg,\n> -                   const char *refname,\n> -                   const struct object_id *old_oid,\n> -                   unsigned int flags)\n> +int refs_delete_ref_with_transaction_flags(struct ref_store *refs,\n> +                                          const char *msg,\n> +                                          const char *refname,\n> +                                          const struct object_id *old_oid,\n> +                                          unsigned int flags,\n> +                                          unsigned int transaction_flags)\n>  {\n>         struct ref_transaction *transaction;\n>         struct strbuf err = STRBUF_INIT;\n>\n> -       transaction = ref_store_transaction_begin(refs, 0, &err);\n> +       transaction = ref_store_transaction_begin(refs, transaction_flags, &err);\n>         if (!transaction ||\n>             ref_transaction_delete(transaction, refname, old_oid,\n>                                    NULL, flags, msg, &err) ||\n> @@ -1027,6 +1029,15 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,\n>         return 0;\n>  }\n>\n> +int refs_delete_ref(struct ref_store *refs, const char *msg,\n> +                   const char *refname,\n> +                   const struct object_id *old_oid,\n> +                   unsigned int flags)\n> +{\n> +       return refs_delete_ref_with_transaction_flags(refs, msg, refname,\n> +                                                     old_oid, flags, 0);\n> +}\n> +\n>  static void copy_reflog_msg(struct strbuf *sb, const char *msg)\n>  {\n>         char c;\n> @@ -1256,11 +1267,20 @@ void ref_transaction_free(struct ref_transaction *transaction)\n>         }\n>\n>         for (i = 0; i < transaction->nr; i++) {\n> +               struct ref_copy_or_rename_update *operation =\n> +                       transaction->updates[i]->copy_or_rename;\n> +\n>                 free(transaction->updates[i]->msg);\n>                 free(transaction->updates[i]->committer_info);\n>                 free((char *)transaction->updates[i]->new_target);\n>                 free((char *)transaction->updates[i]->old_target);\n>                 free((char *)transaction->updates[i]->rejection_details);\n> +               if (operation) {\n> +                       free(operation->old_refname);\n> +                       free(operation->logmsg);\n> +                       free(operation->destination_target);\n> +                       free(operation);\n> +               }\n>                 free(transaction->updates[i]);\n>         }\n>\n> @@ -1273,6 +1293,23 @@ void ref_transaction_free(struct ref_transaction *transaction)\n>         free(transaction);\n>  }\n>\n> +struct ref_update *ref_transaction_copy_or_rename_update(\n> +       struct ref_transaction *transaction)\n> +{\n> +       struct ref_update *operation = NULL;\n> +       size_t i;\n> +\n> +       for (i = 0; i < transaction->nr; i++) {\n> +               if (!transaction->updates[i]->copy_or_rename)\n> +                       continue;\n> +               if (operation)\n> +                       BUG(\"multiple copy or rename updates in one transaction\");\n> +               operation = transaction->updates[i];\n> +       }\n> +\n> +       return operation;\n> +}\n> +\n>  int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,\n>                                        size_t update_idx,\n>                                        enum ref_transaction_error err,\n> @@ -2710,7 +2747,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,\n>                 return REF_TRANSACTION_ERROR_GENERIC;\n>\n>         /* Preparing checks before locking references */\n> -       ret = run_transaction_hook(transaction, \"preparing\");\n> +       ret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :\n> +               run_transaction_hook(transaction, \"preparing\");\n>         if (ret) {\n>                 ref_transaction_abort(transaction, err);\n>                 die(_(abort_by_ref_transaction_hook), \"preparing\");\n> @@ -2720,7 +2758,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,\n>         if (ret)\n>                 return ret;\n>\n> -       ret = run_transaction_hook(transaction, \"prepared\");\n> +       ret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :\n> +               run_transaction_hook(transaction, \"prepared\");\n>         if (ret) {\n>                 ref_transaction_abort(transaction, err);\n>                 die(_(abort_by_ref_transaction_hook), \"prepared\");\n> @@ -2750,7 +2789,8 @@ int ref_transaction_abort(struct ref_transaction *transaction,\n>                 break;\n>         }\n>\n> -       run_transaction_hook(transaction, \"aborted\");\n> +       if (!(transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK))\n> +               run_transaction_hook(transaction, \"aborted\");\n>\n>         ref_transaction_free(transaction);\n>         return ret;\n> @@ -2781,7 +2821,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,\n>         }\n>\n>         ret = refs->be->transaction_finish(refs, transaction, err);\n> -       if (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL))\n> +       if (!ret && !(transaction->flags & (REF_TRANSACTION_FLAG_INITIAL |\n> +                                        REF_TRANSACTION_FLAG_SKIP_HOOK)))\n>                 run_transaction_hook(transaction, \"committed\");\n>         return ret;\n>  }\n> @@ -3123,28 +3164,102 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n>         return ret;\n>  }\n>\n> -int refs_rename_ref(struct ref_store *refs, const char *oldref,\n> -                   const char *newref, const char *logmsg)\n> +static int refs_copy_or_rename_ref(struct ref_store *refs, const char *oldref,\n> +                                  const char *newref, const char *logmsg,\n> +                                  bool copy)\n>  {\n> -       char *msg;\n> -       int retval;\n> +       struct ref_transaction *transaction = NULL;\n> +       struct ref_copy_or_rename_update *operation = NULL;\n> +       struct ref_update *destination_update;\n> +       struct object_id old_oid, new_oid;\n> +       struct strbuf new_target = STRBUF_INIT;\n> +       struct strbuf err = STRBUF_INIT;\n> +       char *msg = normalize_reflog_message(logmsg);\n> +       int old_flags, new_flags = 0, new_exists = 0, ret = 1;\n>\n> -       msg = normalize_reflog_message(logmsg);\n> -       retval = refs->be->rename_ref(refs, oldref, newref, msg);\n> +       if (!strcmp(oldref, newref)) {\n> +               ret = 0;\n> +               goto out;\n> +       }\n> +\n> +       if (!refs_resolve_ref_unsafe(refs, oldref,\n> +                                    RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n> +                                    &old_oid, &old_flags)) {\n> +               error(\"refname %s not found\", oldref);\n> +               goto out;\n> +       }\n> +       if (old_flags & REF_ISSYMREF) {\n> +               error(\"refname %s is a symbolic ref, %s it is not supported\",\n> +                     oldref, copy ? \"copying\" : \"renaming\");\n> +               goto out;\n> +       }\n> +\n> +       transaction = ref_store_transaction_begin(refs, 0, &err);\n> +       if (!transaction)\n> +               goto error;\n> +       if (!copy && ref_transaction_delete(transaction, oldref, &old_oid, NULL,\n> +                                           REF_NO_DEREF, msg, &err))\n> +               goto error;\n> +\n> +       if (refs_resolve_ref_unsafe(refs, newref,\n> +                                   RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n> +                                   &new_oid, &new_flags)) {\n> +               new_exists = 1;\n> +               if ((new_flags & REF_ISSYMREF) &&\n> +                   refs_read_symbolic_ref(refs, newref, &new_target) < 0) {\n> +                       strbuf_addf(&err, \"unable to read symbolic ref %s\", newref);\n> +                       goto error;\n> +               }\n> +       } else {\n> +               oidclr(&new_oid, refs->repo->hash_algo);\n> +       }\n> +       if (ref_transaction_update(transaction, newref, &old_oid,\n> +                                  (new_flags & REF_ISSYMREF) ? NULL : &new_oid,\n> +                                  NULL,\n> +                                  (new_flags & REF_ISSYMREF) ? new_target.buf : NULL,\n> +                                  REF_NO_DEREF | REF_SKIP_CREATE_REFLOG,\n> +                                  NULL, &err))\n> +               goto error;\n> +\n> +       destination_update = transaction->updates[transaction->nr - 1];\n> +       CALLOC_ARRAY(operation, 1);\n> +       operation->type = copy ? REF_UPDATE_COPY : REF_UPDATE_RENAME;\n> +       operation->old_refname = xstrdup(oldref);\n> +       operation->logmsg = xstrdup(msg);\n> +       oidcpy(&operation->source_oid, &old_oid);\n> +       operation->destination_exists = new_exists;\n> +       if (new_flags & REF_ISSYMREF)\n> +               operation->destination_target = xstrdup(new_target.buf);\n> +       else if (operation->destination_exists)\n> +               oidcpy(&operation->destination_oid, &new_oid);\n> +       destination_update->copy_or_rename = operation;\n> +\n> +       if (ref_transaction_commit(transaction, &err))\n> +               goto error;\n> +\n> +       ret = 0;\n> +       goto out;\n> +\n> +error:\n> +       error(\"%s\", err.buf);\n> +out:\n> +       ref_transaction_free(transaction);\n> +       strbuf_release(&new_target);\n> +       strbuf_release(&err);\n>         free(msg);\n> -       return retval;\n> +       return ret;\n>  }\n>\n> -int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,\n> +int refs_rename_ref(struct ref_store *refs, const char *oldref,\n>                     const char *newref, const char *logmsg)\n>  {\n> -       char *msg;\n> -       int retval;\n> +       return refs_copy_or_rename_ref(refs, oldref, newref, logmsg, 0);\n> +}\n>\n> -       msg = normalize_reflog_message(logmsg);\n> -       retval = refs->be->copy_ref(refs, oldref, newref, msg);\n> -       free(msg);\n> -       return retval;\n> +int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,\n> +                   const char *newref, const char *logmsg)\n> +{\n> +       return refs_copy_or_rename_ref(refs, oldref, newref, logmsg, 1);\n>  }\n>\n>  const char *ref_update_original_update_refname(struct ref_update *update)\n> diff --git a/refs/debug.c b/refs/debug.c\n> index 639db0f26..87b84e767 100644\n> --- a/refs/debug.c\n> +++ b/refs/debug.c\n> @@ -143,28 +143,6 @@ static int debug_optimize_required(struct ref_store *ref_store,\n>         return res;\n>  }\n>\n> -static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,\n> -                           const char *newref, const char *logmsg)\n> -{\n> -       struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;\n> -       int res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref,\n> -                                             logmsg);\n> -       trace_printf_key(&trace_refs, \"rename_ref: %s -> %s \\\"%s\\\": %d\\n\", oldref, newref,\n> -               logmsg, res);\n> -       return res;\n> -}\n> -\n> -static int debug_copy_ref(struct ref_store *ref_store, const char *oldref,\n> -                         const char *newref, const char *logmsg)\n> -{\n> -       struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;\n> -       int res =\n> -               drefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg);\n> -       trace_printf_key(&trace_refs, \"copy_ref: %s -> %s \\\"%s\\\": %d\\n\", oldref, newref,\n> -               logmsg, res);\n> -       return res;\n> -}\n> -\n>  struct debug_ref_iterator {\n>         struct ref_iterator base;\n>         struct ref_iterator *iter;\n> @@ -453,9 +431,6 @@ struct ref_storage_be refs_be_debug = {\n>         .optimize = debug_optimize,\n>         .optimize_required = debug_optimize_required,\n>\n> -       .rename_ref = debug_rename_ref,\n> -       .copy_ref = debug_copy_ref,\n> -\n>         .iterator_begin = debug_ref_iterator_begin,\n>         .read_raw_ref = debug_read_raw_ref,\n>         .read_symbolic_ref = debug_read_symbolic_ref,\n> diff --git a/refs/files-backend.c b/refs/files-backend.c\n> index 71628550f..c28228116 100644\n> --- a/refs/files-backend.c\n> +++ b/refs/files-backend.c\n> @@ -1594,6 +1594,7 @@ static int files_optimize_required(struct ref_store *ref_store,\n>   * live into logs/refs.\n>   */\n>  #define TMP_RENAMED_LOG  \"refs/.tmp-renamed-log\"\n> +#define TMP_RENAMED_LOG_DESTINATION \"refs/.tmp-renamed-log-destination\"\n>\n>  struct rename_cb {\n>         const char *tmp_renamed_log;\n> @@ -1685,12 +1686,28 @@ static int refs_rename_ref_available(struct ref_store *refs,\n>         return ok;\n>  }\n>\n> +struct files_copy_or_rename_transaction_data {\n> +       struct ref_lock *lock;\n> +       struct object_id orig_oid;\n> +       struct object_id destination_oid;\n> +       char *destination_target;\n> +       int logmoved;\n> +       int destination_exists;\n> +       int destination_log_backed_up;\n> +};\n> +\n>  static int files_copy_or_rename_ref(struct ref_store *ref_store,\n> -                           const char *oldrefname, const char *newrefname,\n> -                           const char *logmsg, int copy)\n> +                                   struct ref_update *update,\n> +                                   struct ref_transaction *transaction)\n>  {\n>         struct files_ref_store *refs =\n> -               files_downcast(ref_store, REF_STORE_WRITE, \"rename_ref\");\n> +               files_downcast(ref_store, REF_STORE_WRITE,\n> +                              \"ref_transaction_prepare\");\n> +       struct ref_copy_or_rename_update *operation = update->copy_or_rename;\n> +       const char *oldrefname = operation->old_refname;\n> +       const char *newrefname = update->refname;\n> +       const char *logmsg = operation->logmsg;\n> +       bool copy = operation->type == REF_UPDATE_COPY;\n>         struct object_id orig_oid;\n>         int flag = 0, logmoved = 0;\n>         struct ref_lock *lock;\n> @@ -1698,12 +1715,19 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n>         struct strbuf sb_oldref = STRBUF_INIT;\n>         struct strbuf sb_newref = STRBUF_INIT;\n>         struct strbuf tmp_renamed_log = STRBUF_INIT;\n> +       struct strbuf tmp_destination_log = STRBUF_INIT;\n> +       struct strbuf destination_target = STRBUF_INIT;\n>         int log, ret;\n> +       int destination_exists = 0, destination_flags = 0;\n> +       int destination_log_backed_up = 0;\n> +       struct object_id destination_oid;\n> +       struct files_copy_or_rename_transaction_data *data;\n>         struct strbuf err = STRBUF_INIT;\n>\n>         files_reflog_path(refs, &sb_oldref, oldrefname);\n>         files_reflog_path(refs, &sb_newref, newrefname);\n>         files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);\n> +       files_reflog_path(refs, &tmp_destination_log, TMP_RENAMED_LOG_DESTINATION);\n>\n>         log = !lstat(sb_oldref.buf, &loginfo);\n>         if (log && S_ISLNK(loginfo.st_mode)) {\n> @@ -1727,11 +1751,67 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n>                                     oldrefname);\n>                 goto out;\n>         }\n> +       if (!oideq(&orig_oid, &operation->source_oid)) {\n> +               ret = error(\"refname %s is at %s but expected %s\",\n> +                           oldrefname, oid_to_hex(&orig_oid),\n> +                           oid_to_hex(&operation->source_oid));\n> +               goto out;\n> +       }\n>         if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {\n>                 ret = 1;\n>                 goto out;\n>         }\n>\n> +       if (refs_resolve_ref_unsafe(&refs->base, newrefname,\n> +                                   RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n> +                                   &destination_oid, &destination_flags)) {\n> +               destination_exists = 1;\n> +               if ((destination_flags & REF_ISSYMREF) &&\n> +                   refs_read_symbolic_ref(&refs->base, newrefname,\n> +                                          &destination_target) < 0) {\n> +                       ret = error(\"unable to read symbolic ref %s\", newrefname);\n> +                       goto out;\n> +               }\n> +       }\n> +       if (destination_exists != operation->destination_exists) {\n> +               ret = error(\"refname %s changed while renaming\", newrefname);\n> +               goto out;\n> +       }\n> +       if (destination_exists) {\n> +               if (destination_flags & REF_ISSYMREF) {\n> +                       if (!operation->destination_target ||\n> +                           strcmp(destination_target.buf,\n> +                                  operation->destination_target)) {\n> +                               ret = error(\"refname %s changed while renaming\",\n> +                                           newrefname);\n> +                               goto out;\n> +                       }\n> +               } else if (operation->destination_target ||\n> +                          !oideq(&destination_oid,\n> +                                 &operation->destination_oid)) {\n> +                       ret = error(\"refname %s changed while renaming\", newrefname);\n> +                       goto out;\n> +               }\n> +       }\n> +\n> +       if (!lstat(sb_newref.buf, &loginfo)) {\n> +               if (S_ISLNK(loginfo.st_mode)) {\n> +                       ret = error(\"reflog for %s is a symlink\", newrefname);\n> +                       goto out;\n> +               }\n> +               if (S_ISREG(loginfo.st_mode)) {\n> +                       if (copy_file(refs->base.repo, tmp_destination_log.buf,\n> +                                     sb_newref.buf, 0644)) {\n> +                               if (errno != EEXIST)\n> +                                       unlink(tmp_destination_log.buf);\n> +                               ret = error(\"unable to back up logfile logs/%s: %s\",\n> +                                           newrefname, strerror(errno));\n> +                               goto out;\n> +                       }\n> +                       destination_log_backed_up = 1;\n> +               }\n> +       }\n> +\n>         if (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {\n>                 ret = error(\"unable to move logfile logs/%s to logs/\"TMP_RENAMED_LOG\": %s\",\n>                             oldrefname, strerror(errno));\n> @@ -1744,8 +1824,10 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n>                 goto out;\n>         }\n>\n> -       if (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,\n> -                           &orig_oid, REF_NO_DEREF)) {\n> +       if (!copy && refs_delete_ref_with_transaction_flags(&refs->base, logmsg,\n> +                                                        oldrefname, &orig_oid,\n> +                                                        REF_NO_DEREF,\n> +                                                        REF_TRANSACTION_FLAG_SKIP_HOOK)) {\n>                 error(\"unable to delete old %s\", oldrefname);\n>                 goto rollback;\n>         }\n> @@ -1760,8 +1842,9 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n>         if (!copy && refs_resolve_ref_unsafe(&refs->base, newrefname,\n>                                              RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n>                                              NULL, NULL) &&\n> -           refs_delete_ref(&refs->base, NULL, newrefname,\n> -                           NULL, REF_NO_DEREF)) {\n> +           refs_delete_ref_with_transaction_flags(&refs->base, NULL, newrefname,\n> +                                                    NULL, REF_NO_DEREF,\n> +                                                    REF_TRANSACTION_FLAG_SKIP_HOOK)) {\n>                 if (errno == EISDIR) {\n>                         struct strbuf path = STRBUF_INIT;\n>                         int result;\n> @@ -1796,13 +1879,25 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n>         }\n>         oidcpy(&lock->old_oid, &orig_oid);\n>\n> -       if (write_ref_to_lockfile(refs, lock, &orig_oid, &err) ||\n> -           commit_ref_update(refs, lock, &orig_oid, logmsg, 0, &err)) {\n> +       if (write_ref_to_lockfile(refs, lock, &orig_oid, &err)) {\n>                 error(\"unable to write current sha1 into %s: %s\", newrefname, err.buf);\n>                 strbuf_release(&err);\n>                 goto rollback;\n>         }\n>\n> +       CALLOC_ARRAY(data, 1);\n> +       data->lock = lock;\n> +       oidcpy(&data->orig_oid, &orig_oid);\n> +       data->logmoved = logmoved;\n> +       data->destination_exists = destination_exists;\n> +       data->destination_log_backed_up = destination_log_backed_up;\n> +       if (destination_exists && !(destination_flags & REF_ISSYMREF))\n> +               oidcpy(&data->destination_oid, &destination_oid);\n> +       if (destination_flags & REF_ISSYMREF)\n> +               data->destination_target = strbuf_detach(&destination_target, NULL);\n> +       transaction->backend_data = data;\n> +       transaction->state = REF_TRANSACTION_PREPARED;\n> +\n>         ret = 0;\n>         goto out;\n>\n> @@ -1821,38 +1916,40 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,\n>         }\n>\n>   rollbacklog:\n> -       if (logmoved && rename(sb_newref.buf, sb_oldref.buf))\n> -               error(\"unable to restore logfile %s from %s: %s\",\n> -                       oldrefname, newrefname, strerror(errno));\n> +       if (logmoved) {\n> +               if (rename(sb_newref.buf, tmp_renamed_log.buf)) {\n> +                       error(\"unable to restore logfile %s from %s: %s\",\n> +                             oldrefname, newrefname, strerror(errno));\n> +               } else {\n> +                       try_remove_empty_parents(refs, newrefname,\n> +                                                REMOVE_EMPTY_PARENTS_REFLOG);\n> +                       if (rename_tmp_log(refs, oldrefname))\n> +                               error(\"unable to restore logfile %s from logs/\"\n> +                                     TMP_RENAMED_LOG \": %s\",\n> +                                     oldrefname, strerror(errno));\n> +               }\n> +       }\n>         if (!logmoved && log &&\n>             rename(tmp_renamed_log.buf, sb_oldref.buf))\n>                 error(\"unable to restore logfile %s from logs/\"TMP_RENAMED_LOG\": %s\",\n>                         oldrefname, strerror(errno));\n> +       if (destination_log_backed_up &&\n> +           rename(tmp_destination_log.buf, sb_newref.buf))\n> +               error(\"unable to restore logfile %s: %s\",\n> +                     newrefname, strerror(errno));\n>         ret = 1;\n>   out:\n> +       if (ret && destination_log_backed_up)\n> +               unlink(tmp_destination_log.buf);\n>         strbuf_release(&sb_newref);\n>         strbuf_release(&sb_oldref);\n>         strbuf_release(&tmp_renamed_log);\n> +       strbuf_release(&tmp_destination_log);\n> +       strbuf_release(&destination_target);\n>\n>         return ret;\n>  }\n>\n> -static int files_rename_ref(struct ref_store *ref_store,\n> -                           const char *oldrefname, const char *newrefname,\n> -                           const char *logmsg)\n> -{\n> -       return files_copy_or_rename_ref(ref_store, oldrefname,\n> -                                newrefname, logmsg, 0);\n> -}\n> -\n> -static int files_copy_ref(struct ref_store *ref_store,\n> -                           const char *oldrefname, const char *newrefname,\n> -                           const char *logmsg)\n> -{\n> -       return files_copy_or_rename_ref(ref_store, oldrefname,\n> -                                newrefname, logmsg, 1);\n> -}\n> -\n>  static int close_ref_gently(struct ref_lock *lock)\n>  {\n>         if (close_lock_file_gently(&lock->lk))\n> @@ -2962,6 +3059,14 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n>         struct ref_transaction *packed_transaction = NULL;\n>\n>         assert(err);\n> +       {\n> +               struct ref_update *operation =\n> +                       ref_transaction_copy_or_rename_update(transaction);\n> +\n> +               if (operation)\n> +                       return files_copy_or_rename_ref(ref_store, operation,\n> +                                                       transaction);\n> +       }\n>\n>         if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n>                 goto cleanup;\n> @@ -3318,6 +3423,10 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,\n>         return ret;\n>  }\n>\n> +static int files_transaction_abort(struct ref_store *ref_store,\n> +                                  struct ref_transaction *transaction,\n> +                                  struct strbuf *err);\n> +\n>  static int files_transaction_finish(struct ref_store *ref_store,\n>                                     struct ref_transaction *transaction,\n>                                     struct strbuf *err)\n> @@ -3333,6 +3442,40 @@ static int files_transaction_finish(struct ref_store *ref_store,\n>\n>\n>         assert(err);\n> +       {\n> +               struct ref_update *update =\n> +                       ref_transaction_copy_or_rename_update(transaction);\n> +\n> +               if (update) {\n> +                       struct ref_copy_or_rename_update *operation =\n> +                               update->copy_or_rename;\n> +                       struct files_copy_or_rename_transaction_data *data =\n> +                               transaction->backend_data;\n> +                       int special_ret;\n> +\n> +                       special_ret = commit_ref_update(refs, data->lock, &data->orig_oid,\n> +                                                       operation->logmsg, 0, err);\n> +                       if (special_ret) {\n> +                               error(\"unable to write current sha1 into %s: %s\",\n> +                                     update->refname, err->buf);\n> +                               data->lock = NULL;\n> +                               files_transaction_abort(ref_store, transaction, err);\n> +                               return special_ret;\n> +                       } else if (data->destination_log_backed_up) {\n> +                               struct strbuf path = STRBUF_INIT;\n> +\n> +                               files_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);\n> +                               if (unlink(path.buf) < 0 && errno != ENOENT)\n> +                                       warning_errno(\"unable to remove '%s'\", path.buf);\n> +                               strbuf_release(&path);\n> +                       }\n> +                       free(data->destination_target);\n> +                       free(data);\n> +                       transaction->backend_data = NULL;\n> +                       transaction->state = REF_TRANSACTION_CLOSED;\n> +                       return special_ret;\n> +               }\n> +       }\n>\n>         if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n>                 return files_transaction_finish_initial(refs, transaction, err);\n> @@ -3476,11 +3619,105 @@ static int files_transaction_finish(struct ref_store *ref_store,\n>\n>  static int files_transaction_abort(struct ref_store *ref_store,\n>                                    struct ref_transaction *transaction,\n> -                                  struct strbuf *err UNUSED)\n> +                                  struct strbuf *err)\n>  {\n>         struct files_ref_store *refs =\n>                 files_downcast(ref_store, 0, \"ref_transaction_abort\");\n>\n> +       {\n> +               struct ref_update *update =\n> +                       ref_transaction_copy_or_rename_update(transaction);\n> +\n> +               if (update) {\n> +                       struct ref_copy_or_rename_update *operation =\n> +                               update->copy_or_rename;\n> +                       struct files_copy_or_rename_transaction_data *data =\n> +                               transaction->backend_data;\n> +                       struct strbuf new_log = STRBUF_INIT;\n> +                       struct strbuf destination_log = STRBUF_INIT;\n> +                       struct strbuf temporary_log = STRBUF_INIT;\n> +                       struct ref_transaction *restore_transaction = NULL;\n> +                       struct ref_lock *lock;\n> +                       int ret = 0;\n> +\n> +                       if (data->lock)\n> +                               unlock_ref(data->lock);\n> +                       if (operation->type == REF_UPDATE_RENAME) {\n> +                               lock = lock_ref_oid_basic(refs, operation->old_refname, err);\n> +                               if (!lock ||\n> +                                   write_ref_to_lockfile(refs, lock, &data->orig_oid, err) ||\n> +                                   commit_ref_update(refs, lock, &data->orig_oid, NULL,\n> +                                                     REF_SKIP_CREATE_REFLOG, err))\n> +                                       ret = -1;\n> +                       }\n> +\n> +                       if (data->logmoved) {\n> +                               files_reflog_path(refs, &new_log, update->refname);\n> +                               if (operation->type == REF_UPDATE_RENAME) {\n> +                                       files_reflog_path(refs, &temporary_log, TMP_RENAMED_LOG);\n> +                                       if (rename(new_log.buf, temporary_log.buf) < 0) {\n> +                                               strbuf_addf(err, \"unable to restore logfile %s: %s\",\n> +                                                           operation->old_refname, strerror(errno));\n> +                                               ret = -1;\n> +                                       } else {\n> +                                               try_remove_empty_parents(refs,\n> +                                                                        update->refname,\n> +                                                                        REMOVE_EMPTY_PARENTS_REFLOG);\n> +                                               if (rename_tmp_log(refs,\n> +                                                                  operation->old_refname)) {\n> +                                                       strbuf_addf(err, \"unable to restore logfile %s: %s\",\n> +                                                                   operation->old_refname,\n> +                                                                   strerror(errno));\n> +                                                       ret = -1;\n> +                                               }\n> +                                       }\n> +                               } else if (unlink(new_log.buf) < 0 && errno != ENOENT) {\n> +                                       strbuf_addf(err, \"unable to remove logfile %s: %s\",\n> +                                                   update->refname, strerror(errno));\n> +                                       ret = -1;\n> +                               }\n> +                       }\n> +                       if (data->destination_log_backed_up) {\n> +                               files_reflog_path(refs, &destination_log,\n> +                                                 TMP_RENAMED_LOG_DESTINATION);\n> +                               if (rename(destination_log.buf, new_log.buf) < 0) {\n> +                                       strbuf_addf(err, \"unable to restore logfile %s: %s\",\n> +                                                   update->refname, strerror(errno));\n> +                                       ret = -1;\n> +                               }\n> +                       }\n> +\n> +                       if (operation->type == REF_UPDATE_RENAME &&\n> +                           data->destination_exists) {\n> +                               restore_transaction = ref_store_transaction_begin(\n> +                                       &refs->base, REF_TRANSACTION_FLAG_SKIP_HOOK, err);\n> +                               if (!restore_transaction ||\n> +                                   ref_transaction_update(restore_transaction,\n> +                                                          update->refname,\n> +                                                          data->destination_target ? NULL :\n> +                                                                                     &data->destination_oid,\n> +                                                          NULL,\n> +                                                          data->destination_target,\n> +                                                          NULL,\n> +                                                          REF_NO_DEREF |\n> +                                                                  REF_SKIP_CREATE_REFLOG,\n> +                                                          NULL, err) ||\n> +                                   ref_transaction_commit(restore_transaction, err))\n> +                                       ret = -1;\n> +                               ref_transaction_free(restore_transaction);\n> +                       }\n> +\n> +                       strbuf_release(&destination_log);\n> +                       strbuf_release(&temporary_log);\n> +                       strbuf_release(&new_log);\n> +                       free(data->destination_target);\n> +                       free(data);\n> +                       transaction->backend_data = NULL;\n> +                       transaction->state = REF_TRANSACTION_CLOSED;\n> +                       return ret;\n> +               }\n> +       }\n> +\n>         files_transaction_cleanup(refs, transaction);\n>         return 0;\n>  }\n> @@ -4095,8 +4332,6 @@ struct ref_storage_be refs_be_files = {\n>\n>         .optimize = files_optimize,\n>         .optimize_required = files_optimize_required,\n> -       .rename_ref = files_rename_ref,\n> -       .copy_ref = files_copy_ref,\n>\n>         .iterator_begin = files_ref_iterator_begin,\n>         .read_raw_ref = files_read_raw_ref,\n> diff --git a/refs/packed-backend.c b/refs/packed-backend.c\n> index a73fc6aca..364a91291 100644\n> --- a/refs/packed-backend.c\n> +++ b/refs/packed-backend.c\n> @@ -2164,8 +2164,6 @@ struct ref_storage_be refs_be_packed = {\n>         .optimize = packed_optimize,\n>         .optimize_required = packed_optimize_required,\n>\n> -       .rename_ref = NULL,\n> -       .copy_ref = NULL,\n>\n>         .iterator_begin = packed_ref_iterator_begin,\n>         .read_raw_ref = packed_read_raw_ref,\n> diff --git a/refs/refs-internal.h b/refs/refs-internal.h\n> index c3ac7b556..5d4dc0171 100644\n> --- a/refs/refs-internal.h\n> +++ b/refs/refs-internal.h\n> @@ -155,9 +155,33 @@ struct ref_update {\n>          */\n>         struct ref_update *parent_update;\n>\n> +       /*\n> +        * Copy and rename operations require backend-specific handling while\n> +        * still exposing their logical updates to transaction hooks. Keep that\n> +        * state on the destination update so it composes with other updates in\n> +        * the transaction instead of making copy or rename a transaction-wide\n> +        * property.\n> +        */\n> +       struct ref_copy_or_rename_update *copy_or_rename;\n> +\n>         const char refname[FLEX_ARRAY];\n>  };\n>\n> +enum ref_copy_or_rename_type {\n> +       REF_UPDATE_RENAME,\n> +       REF_UPDATE_COPY,\n> +};\n> +\n> +struct ref_copy_or_rename_update {\n> +       enum ref_copy_or_rename_type type;\n> +       char *old_refname;\n> +       char *logmsg;\n> +       struct object_id source_oid;\n> +       struct object_id destination_oid;\n> +       char *destination_target;\n> +       unsigned int destination_exists:1;\n> +};\n> +\n>  int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,\n>                       struct object_id *oid, struct strbuf *referent,\n>                       unsigned int *type, int *failure_errno);\n> @@ -187,6 +211,13 @@ struct ref_update *ref_transaction_add_update(\n>                 const char *committer_info,\n>                 const char *msg);\n>\n> +int refs_delete_ref_with_transaction_flags(struct ref_store *refs,\n> +                                          const char *msg,\n> +                                          const char *refname,\n> +                                          const struct object_id *old_oid,\n> +                                          unsigned int flags,\n> +                                          unsigned int transaction_flags);\n> +\n>  /*\n>   * Transaction states.\n>   *\n> @@ -242,6 +273,12 @@ struct ref_transaction {\n>         uint64_t max_index;\n>  };\n>\n> +/* Suppress hooks for a transaction nested inside another refs operation. */\n> +#define REF_TRANSACTION_FLAG_SKIP_HOOK (1 << 2)\n> +\n> +struct ref_update *ref_transaction_copy_or_rename_update(\n> +       struct ref_transaction *transaction);\n> +\n>  /*\n>   * Check for entries in extras that are within the specified\n>   * directory, where dirname is a reference directory name including\n> @@ -451,13 +488,6 @@ typedef int optimize_required_fn(struct ref_store *ref_store,\n>                                  struct refs_optimize_opts *opts,\n>                                  bool *required);\n>\n> -typedef int rename_ref_fn(struct ref_store *ref_store,\n> -                         const char *oldref, const char *newref,\n> -                         const char *logmsg);\n> -typedef int copy_ref_fn(struct ref_store *ref_store,\n> -                         const char *oldref, const char *newref,\n> -                         const char *logmsg);\n> -\n>  /*\n>   * Iterate over the references in `ref_store` whose names start with\n>   * `prefix`. `prefix` is matched as a literal string, without regard\n> @@ -577,9 +607,6 @@ struct ref_storage_be {\n>\n>         optimize_fn *optimize;\n>         optimize_required_fn *optimize_required;\n> -       rename_ref_fn *rename_ref;\n> -       copy_ref_fn *copy_ref;\n> -\n>         ref_iterator_begin_fn *iterator_begin;\n>         read_raw_ref_fn *read_raw_ref;\n>\n> diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\n> index 10db03991..589fcc998 100644\n> --- a/refs/reftable-backend.c\n> +++ b/refs/reftable-backend.c\n> @@ -953,6 +953,14 @@ struct reftable_transaction_data {\n>         size_t args_nr, args_alloc;\n>  };\n>\n> +struct reftable_copy_or_rename_transaction_data {\n> +       struct reftable_addition *addition;\n> +};\n> +\n> +static int reftable_be_copy_or_rename_prepare(struct ref_store *ref_store,\n> +                                              struct ref_transaction *transaction,\n> +                                              struct strbuf *err);\n> +\n>  static void free_transaction_data(struct reftable_transaction_data *tx_data)\n>  {\n>         if (!tx_data)\n> @@ -1326,6 +1334,10 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n>         size_t i;\n>         int ret;\n>\n> +       if (ref_transaction_copy_or_rename_update(transaction))\n> +               return reftable_be_copy_or_rename_prepare(ref_store, transaction,\n> +                                                          err);\n> +\n>         ret = refs->err;\n>         if (ret < 0)\n>                 goto done;\n> @@ -1419,7 +1431,20 @@ static int reftable_be_transaction_abort(struct ref_store *ref_store UNUSED,\n>                                          struct ref_transaction *transaction,\n>                                          struct strbuf *err UNUSED)\n>  {\n> -       struct reftable_transaction_data *tx_data = transaction->backend_data;\n> +       struct reftable_transaction_data *tx_data;\n> +\n> +       if (ref_transaction_copy_or_rename_update(transaction)) {\n> +               struct reftable_copy_or_rename_transaction_data *data =\n> +                       transaction->backend_data;\n> +\n> +               reftable_addition_destroy(data->addition);\n> +               free(data);\n> +               transaction->backend_data = NULL;\n> +               transaction->state = REF_TRANSACTION_CLOSED;\n> +               return 0;\n> +       }\n> +\n> +       tx_data = transaction->backend_data;\n>         free_transaction_data(tx_data);\n>         transaction->state = REF_TRANSACTION_CLOSED;\n>         return 0;\n> @@ -1667,9 +1692,28 @@ static int reftable_be_transaction_finish(struct ref_store *ref_store UNUSED,\n>                                           struct ref_transaction *transaction,\n>                                           struct strbuf *err)\n>  {\n> -       struct reftable_transaction_data *tx_data = transaction->backend_data;\n> +       struct reftable_transaction_data *tx_data;\n>         int ret = 0;\n>\n> +       if (ref_transaction_copy_or_rename_update(transaction)) {\n> +               struct reftable_copy_or_rename_transaction_data *data =\n> +                       transaction->backend_data;\n> +               int special_ret = reftable_addition_commit(data->addition);\n> +\n> +               reftable_addition_destroy(data->addition);\n> +               free(data);\n> +               transaction->backend_data = NULL;\n> +               transaction->state = REF_TRANSACTION_CLOSED;\n> +               if (special_ret < 0) {\n> +                       strbuf_addf(err, _(\"reftable: transaction failure: %s\"),\n> +                                   reftable_error_str(special_ret));\n> +                       return -1;\n> +               }\n> +               return 0;\n> +       }\n> +\n> +       tx_data = transaction->backend_data;\n> +\n>         for (size_t i = 0; i < tx_data->args_nr; i++) {\n>                 tx_data->args[i].max_index = transaction->max_index;\n>\n> @@ -1764,17 +1808,20 @@ struct write_create_symref_arg {\n>  struct write_copy_arg {\n>         struct reftable_ref_store *refs;\n>         struct reftable_backend *be;\n> +       struct strbuf *err;\n>         const char *oldname;\n>         const char *newname;\n>         const char *logmsg;\n>         int delete_old;\n> +       struct ref_copy_or_rename_update *operation;\n>  };\n>\n>  static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n>  {\n>         struct write_copy_arg *arg = cb_data;\n>         uint64_t deletion_ts, creation_ts;\n> -       struct reftable_ref_record old_ref = {0}, refs[2] = {0};\n> +       struct reftable_ref_record old_ref = {0}, destination_ref = {0};\n> +       struct reftable_ref_record refs[2] = {0};\n>         struct reftable_log_record old_log = {0}, *logs = NULL;\n>         struct reftable_iterator it = {0};\n>         struct string_list skip = STRING_LIST_INIT_NODUP;\n> @@ -1789,14 +1836,75 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n>                 BUG(\"failed splitting committer info\");\n>\n>         if (reftable_stack_read_ref(arg->be->stack, arg->oldname, &old_ref)) {\n> -               ret = error(_(\"refname %s not found\"), arg->oldname);\n> +               strbuf_addf(arg->err, _(\"refname %s not found\"), arg->oldname);\n> +               ret = -1;\n>                 goto done;\n>         }\n>         if (old_ref.value_type == REFTABLE_REF_SYMREF) {\n> -               ret = error(_(\"refname %s is a symbolic ref, copying it is not supported\"),\n> +               strbuf_addf(arg->err,\n> +                           _(\"refname %s is a symbolic ref, copying it is not supported\"),\n>                             arg->oldname);\n> +               ret = -1;\n>                 goto done;\n>         }\n> +       if (arg->operation) {\n> +               struct object_id oid;\n> +\n> +               if (old_ref.value_type == REFTABLE_REF_VAL2)\n> +                       oidread(&oid, old_ref.value.val2.value,\n> +                               arg->refs->base.repo->hash_algo);\n> +               else\n> +                       oidread(&oid, old_ref.value.val1,\n> +                               arg->refs->base.repo->hash_algo);\n> +               if (!oideq(&oid, &arg->operation->source_oid)) {\n> +                       strbuf_addf(arg->err,\n> +                                   _(\"refname %s is at %s but expected %s\"),\n> +                                   arg->oldname, oid_to_hex(&oid),\n> +                                   oid_to_hex(&arg->operation->source_oid));\n> +                       ret = -1;\n> +                       goto done;\n> +               }\n> +\n> +               ret = reftable_stack_read_ref(arg->be->stack, arg->newname,\n> +                                             &destination_ref);\n> +               if (ret < 0)\n> +                       goto done;\n> +               if (arg->operation->destination_exists != !ret) {\n> +                       strbuf_addf(arg->err,\n> +                                   _(\"refname %s changed while renaming\"),\n> +                                   arg->newname);\n> +                       ret = -1;\n> +                       goto done;\n> +               }\n> +               if (!ret) {\n> +                       if (destination_ref.value_type == REFTABLE_REF_SYMREF) {\n> +                               if (!arg->operation->destination_target ||\n> +                                   strcmp(destination_ref.value.symref,\n> +                                          arg->operation->destination_target)) {\n> +                                       strbuf_addf(arg->err,\n> +                                                   _(\"refname %s changed while renaming\"),\n> +                                                   arg->newname);\n> +                                       ret = -1;\n> +                                       goto done;\n> +                               }\n> +                       } else {\n> +                               if (destination_ref.value_type == REFTABLE_REF_VAL2)\n> +                                       oidread(&oid, destination_ref.value.val2.value,\n> +                                               arg->refs->base.repo->hash_algo);\n> +                               else\n> +                                       oidread(&oid, destination_ref.value.val1,\n> +                                               arg->refs->base.repo->hash_algo);\n> +                               if (arg->operation->destination_target ||\n> +                                   !oideq(&oid, &arg->operation->destination_oid)) {\n> +                                       strbuf_addf(arg->err,\n> +                                                   _(\"refname %s changed while renaming\"),\n> +                                                   arg->newname);\n> +                                       ret = -1;\n> +                                       goto done;\n> +                               }\n> +                       }\n> +               }\n> +       }\n>\n>         /*\n>          * There's nothing to do in case the old and new name are the same, so\n> @@ -1815,7 +1923,7 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n>         ret = refs_verify_refname_available(&arg->refs->base, arg->newname,\n>                                             NULL, &skip, 0, &errbuf);\n>         if (ret < 0) {\n> -               error(\"%s\", errbuf.buf);\n> +               strbuf_addbuf(arg->err, &errbuf);\n>                 goto done;\n>         }\n>\n> @@ -1980,68 +2088,63 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n>         for (i = 0; i < ARRAY_SIZE(refs); i++)\n>                 reftable_ref_record_release(&refs[i]);\n>         reftable_ref_record_release(&old_ref);\n> +       reftable_ref_record_release(&destination_ref);\n>         reftable_log_record_release(&old_log);\n>         return ret;\n>  }\n>\n> -static int reftable_be_rename_ref(struct ref_store *ref_store,\n> -                                 const char *oldrefname,\n> -                                 const char *newrefname,\n> -                                 const char *logmsg)\n> +static int reftable_be_copy_or_rename_prepare(struct ref_store *ref_store,\n> +                                              struct ref_transaction *transaction,\n> +                                              struct strbuf *err)\n>  {\n>         struct reftable_ref_store *refs =\n> -               reftable_be_downcast(ref_store, REF_STORE_WRITE, \"rename_ref\");\n> +               reftable_be_downcast(ref_store, REF_STORE_WRITE,\n> +                                    \"ref_transaction_prepare\");\n> +       struct reftable_copy_or_rename_transaction_data *data = NULL;\n> +       struct ref_update *update =\n> +               ref_transaction_copy_or_rename_update(transaction);\n> +       struct ref_copy_or_rename_update *operation = update->copy_or_rename;\n>         struct write_copy_arg arg = {\n>                 .refs = refs,\n> -               .oldname = oldrefname,\n> -               .newname = newrefname,\n> -               .logmsg = logmsg,\n> -               .delete_old = 1,\n> +               .err = err,\n> +               .oldname = operation->old_refname,\n> +               .newname = update->refname,\n> +               .logmsg = operation->logmsg,\n> +               .delete_old = operation->type == REF_UPDATE_RENAME,\n> +               .operation = operation,\n>         };\n>         int ret;\n>\n> +       CALLOC_ARRAY(data, 1);\n>         ret = refs->err;\n>         if (ret < 0)\n>                 goto done;\n> -\n> -       ret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);\n> +       ret = backend_for(&arg.be, refs, update->refname,\n> +                         &arg.newname, 1);\n>         if (ret)\n>                 goto done;\n> -       ret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,\n> -                                &reftable_be_write_options(refs)->opts);\n> -\n> -done:\n> -       assert(ret != REFTABLE_API_ERROR);\n> -       return ret;\n> -}\n> -\n> -static int reftable_be_copy_ref(struct ref_store *ref_store,\n> -                               const char *oldrefname,\n> -                               const char *newrefname,\n> -                               const char *logmsg)\n> -{\n> -       struct reftable_ref_store *refs =\n> -               reftable_be_downcast(ref_store, REF_STORE_WRITE, \"copy_ref\");\n> -       struct write_copy_arg arg = {\n> -               .refs = refs,\n> -               .oldname = oldrefname,\n> -               .newname = newrefname,\n> -               .logmsg = logmsg,\n> -       };\n> -       int ret;\n> -\n> -       ret = refs->err;\n> -       if (ret < 0)\n> +       ret = reftable_stack_addition_new(&data->addition, arg.be->stack,\n> +                                         &reftable_be_write_options(refs)->opts);\n> +       if (ret)\n>                 goto done;\n> -\n> -       ret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);\n> +       ret = reftable_addition_add(data->addition, &write_copy_table, &arg);\n>         if (ret)\n>                 goto done;\n> -       ret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,\n> -                                &reftable_be_write_options(refs)->opts);\n> +\n> +       transaction->backend_data = data;\n> +       transaction->state = REF_TRANSACTION_PREPARED;\n> +       return 0;\n>\n>  done:\n>         assert(ret != REFTABLE_API_ERROR);\n> +       if (data) {\n> +               reftable_addition_destroy(data->addition);\n> +               free(data);\n> +       }\n> +       transaction->state = REF_TRANSACTION_CLOSED;\n> +       if (ret && !err->len)\n> +               strbuf_addf(err, _(\"reftable: transaction prepare: %s\"),\n> +                           reftable_error_str(ret));\n>         return ret;\n>  }\n>\n> @@ -2872,8 +2975,6 @@ struct ref_storage_be refs_be_reftable = {\n>         .optimize = reftable_be_optimize,\n>         .optimize_required = reftable_be_optimize_required,\n>\n> -       .rename_ref = reftable_be_rename_ref,\n> -       .copy_ref = reftable_be_copy_ref,\n>\n>         .iterator_begin = reftable_be_iterator_begin,\n>         .read_raw_ref = reftable_be_read_raw_ref,\n> diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh\n> index 4fe9d9b23..116b2ff07 100755\n> --- a/t/t1416-ref-transaction-hooks.sh\n> +++ b/t/t1416-ref-transaction-hooks.sh\n> @@ -93,6 +93,148 @@ test_expect_success 'hook gets all queued updates in committed state' '\n>         test_cmp expect actual\n>  '\n>\n> +test_expect_success 'hook gets both updates when renaming a branch' '\n> +       test_when_finished \"rm -f actual\" &&\n> +       git branch old PRE &&\n> +       test_hook reference-transaction <<-\\EOF &&\n> +               echo \"$1\" >>actual &&\n> +               cat >>actual\n> +       EOF\n> +       cat >expect <<-EOF &&\n> +       preparing\n> +       $PRE_OID $ZERO_OID refs/heads/old\n> +       $ZERO_OID $PRE_OID refs/heads/new\n> +       prepared\n> +       $PRE_OID $ZERO_OID refs/heads/old\n> +       $ZERO_OID $PRE_OID refs/heads/new\n> +       committed\n> +       $PRE_OID $ZERO_OID refs/heads/old\n> +       $ZERO_OID $PRE_OID refs/heads/new\n> +       EOF\n> +       git branch -m old new &&\n> +       test_cmp expect actual &&\n> +       test_must_fail git rev-parse --verify refs/heads/old &&\n> +       test_cmp_rev PRE refs/heads/new\n> +'\n> +\n> +test_expect_success 'hook gets destination update when copying a branch' '\n> +       test_when_finished \"rm -f actual\" &&\n> +       git branch copy-source PRE &&\n> +       test_hook reference-transaction <<-\\EOF &&\n> +               echo \"$1\" >>actual &&\n> +               cat >>actual\n> +       EOF\n> +       cat >expect <<-EOF &&\n> +       preparing\n> +       $ZERO_OID $PRE_OID refs/heads/copy-destination\n> +       prepared\n> +       $ZERO_OID $PRE_OID refs/heads/copy-destination\n> +       committed\n> +       $ZERO_OID $PRE_OID refs/heads/copy-destination\n> +       EOF\n> +       git branch -c copy-source copy-destination &&\n> +       test_cmp expect actual &&\n> +       test_cmp_rev PRE refs/heads/copy-source &&\n> +       test_cmp_rev PRE refs/heads/copy-destination\n> +'\n> +\n> +test_expect_success 'hook gets overwritten values for forced rename and copy' '\n> +       git branch force-old PRE &&\n> +       git branch force-new POST &&\n> +       git branch force-copy-source PRE &&\n> +       git branch force-copy-destination POST &&\n> +       test_hook reference-transaction <<-\\EOF &&\n> +               if test \"$1\" = committed\n> +               then\n> +                       cat >>actual\n> +               fi\n> +       EOF\n> +       git branch -M force-old force-new &&\n> +       git branch -C force-copy-source force-copy-destination &&\n> +       cat >expect <<-EOF &&\n> +       $PRE_OID $ZERO_OID refs/heads/force-old\n> +       $POST_OID $PRE_OID refs/heads/force-new\n> +       $POST_OID $PRE_OID refs/heads/force-copy-destination\n> +       EOF\n> +       test_cmp expect actual\n> +'\n> +\n> +test_expect_success 'hook can abort a branch rename after preparation' '\n> +       git branch abort-old PRE &&\n> +       git branch abort-new POST &&\n> +       git reflog show --format=%gs abort-old >old-log &&\n> +       git reflog show --format=%gs abort-new >new-log &&\n> +       test_hook reference-transaction <<-\\EOF &&\n> +               test \"$1\" != prepared\n> +       EOF\n> +       test_must_fail git branch -M abort-old abort-new &&\n> +       test_cmp_rev PRE refs/heads/abort-old &&\n> +       test_cmp_rev POST refs/heads/abort-new &&\n> +       git reflog show --format=%gs abort-old >old-log-after &&\n> +       git reflog show --format=%gs abort-new >new-log-after &&\n> +       test_cmp old-log old-log-after &&\n> +       test_cmp new-log new-log-after\n> +'\n> +\n> +test_expect_success 'hook can abort a D/F branch rename after preparation' '\n> +       git branch df-old PRE &&\n> +       git reflog show --format=%gs df-old >df-log &&\n> +       test_hook reference-transaction <<-\\EOF &&\n> +               test \"$1\" != prepared\n> +       EOF\n> +       test_must_fail git branch -m df-old df-old/child &&\n> +       test_cmp_rev PRE refs/heads/df-old &&\n> +       test_must_fail git rev-parse --verify refs/heads/df-old/child &&\n> +       git reflog show --format=%gs df-old >df-log-after &&\n> +       test_cmp df-log df-log-after\n> +'\n> +\n> +test_expect_success 'hook can abort a reverse D/F rename after preparation' '\n> +       git branch reverse/old PRE &&\n> +       git reflog show --format=%gs reverse/old >reverse-log &&\n> +       test_hook reference-transaction <<-\\EOF &&\n> +               test \"$1\" != prepared\n> +       EOF\n> +       test_must_fail git branch -m reverse/old reverse &&\n> +       test_cmp_rev PRE refs/heads/reverse/old &&\n> +       test_must_fail git rev-parse --verify refs/heads/reverse &&\n> +       git reflog show --format=%gs reverse/old >reverse-log-after &&\n> +       test_cmp reverse-log reverse-log-after\n> +'\n> +\n> +test_expect_success 'hook can abort a forced branch copy after preparation' '\n> +       git branch copy-abort-old PRE &&\n> +       git branch copy-abort-new POST &&\n> +       git reflog show --format=%gs copy-abort-old >copy-old-log &&\n> +       git reflog show --format=%gs copy-abort-new >copy-new-log &&\n> +       test_hook reference-transaction <<-\\EOF &&\n> +               test \"$1\" != prepared\n> +       EOF\n> +       test_must_fail git branch -C copy-abort-old copy-abort-new &&\n> +       test_cmp_rev PRE refs/heads/copy-abort-old &&\n> +       test_cmp_rev POST refs/heads/copy-abort-new &&\n> +       git reflog show --format=%gs copy-abort-old >copy-old-log-after &&\n> +       git reflog show --format=%gs copy-abort-new >copy-new-log-after &&\n> +       test_cmp copy-old-log copy-old-log-after &&\n> +       test_cmp copy-new-log copy-new-log-after\n> +'\n> +\n> +test_expect_success 'branch rename detects an update during preparing hook' '\n> +       git branch race-old PRE &&\n> +       git branch race-new POST &&\n> +       test_hook reference-transaction <<-\\EOF &&\n> +               marker=$(git rev-parse --git-path rename-race-once)\n> +               if test \"$1\" = preparing && test ! -e \"$marker\"\n> +               then\n> +                       >\"$marker\" &&\n> +                       git update-ref refs/heads/race-old POST\n> +               fi\n> +       EOF\n> +       test_must_fail git branch -M race-old race-new &&\n> +       test_cmp_rev POST refs/heads/race-old &&\n> +       test_cmp_rev POST refs/heads/race-new\n> +'\n> +\n>  test_expect_success 'hook gets all queued updates in aborted state' '\n>         test_when_finished \"rm actual\" &&\n>         git reset --hard PRE &&\n> --\n> 2.39.3 (Apple Git-146)\n"},{"id":"553948","messageId":"ar-N7SA63fN_xx9P@pks.im","threadId":"66350","inReplyTo":"20260923133651.74120-1-maciej.ciemborowicz@gmail.com","subject":"Re: [PATCH v2] refs: run copy and rename through transactions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-02T10:56:45Z","receivedAt":"2026-10-02T10:56:53Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Wed, Sep 23, 2026 at 03:36:51PM +0200, Maciej Ciemborowicz wrote:\n> Reference copy and rename operations bypass the transaction API.\n> Consequently, the reference-transaction hook sees only the source deletion\n> with the files backend and no useful update with the reftable backend.\n> \n> Represent both operations as reference transactions containing their\n> logical updates. A rename is a deletion of the old reference and creation\n> of the new reference in the same transaction. Attach operation-specific\n> state to the destination update instead of making copy or rename a property\n> of the entire transaction.\n\nSorry, but what does this last sentence mean? What is the consequence\nof it?\n\n> Retain backend-specific reflog handling: the files backend stages its\n> existing rename procedure across prepare, finish and abort, while reftable\n> stages an addition while holding the stack lock. Suppress hooks for the\n> files backend's nested deletion transactions so that callers observe one\n> logical transaction.\n\nThe fact that we retain the backend-specific logic is not really\ninteresting by itself. The way more interesting question is _why_ we\nretain it. Or asked differently, why can't we make this whole mechanism\ncompletely agnostic of the backend and implement this via pure\ntransactions?\n\n> Record and verify the source and destination values after taking backend\n> locks. This rejects concurrent changes instead of applying a rename or copy\n> that differs from the payload shown to the preparing hook. Preserve D/F\n> renames and restore overwritten references and reflogs when a prepared hook\n> rejects the operation.\n\nIs this new behaviour? Is this retaining old behaviour? I have no clue.\n\n> Add tests covering rename, copy, forced updates, both directions of D/F\n> conflicts, concurrent updates and prepared-hook rollback.\n\nThis sentence doesn't really add much value to the message.\n\nHow does all of this impact performance?\n\n> diff --git a/refs.c b/refs.c\n> index 92d5df5b7..f036ae4b9 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -1027,6 +1029,15 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,\n>  \treturn 0;\n>  }\n>  \n> +int refs_delete_ref(struct ref_store *refs, const char *msg,\n> +\t\t    const char *refname,\n> +\t\t    const struct object_id *old_oid,\n> +\t\t    unsigned int flags)\n> +{\n> +\treturn refs_delete_ref_with_transaction_flags(refs, msg, refname,\n> +\t\t\t\t\t\t      old_oid, flags, 0);\n> +}\n> +\n>  static void copy_reflog_msg(struct strbuf *sb, const char *msg)\n>  {\n>  \tchar c;\n\nRefactorings like these could easily go into a separate commit to make\nthis easier to review.\n\n> @@ -2710,7 +2747,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,\n>  \t\treturn REF_TRANSACTION_ERROR_GENERIC;\n>  \n>  \t/* Preparing checks before locking references */\n> -\tret = run_transaction_hook(transaction, \"preparing\");\n> +\tret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :\n> +\t\trun_transaction_hook(transaction, \"preparing\");\n>  \tif (ret) {\n>  \t\tref_transaction_abort(transaction, err);\n>  \t\tdie(_(abort_by_ref_transaction_hook), \"preparing\");\n\nInstead of teaching every site to conditionally call\n`run_transaction_hook()` only when the flag is not set, can't we adapt\nthe function itself to skip?\n\nIn any case, this is another change that could easily be split out into\na separate commit.\n\n> diff --git a/refs/files-backend.c b/refs/files-backend.c\n> index 71628550f..c28228116 100644\n> --- a/refs/files-backend.c\n> +++ b/refs/files-backend.c\n> @@ -2962,6 +3059,14 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n>  \tstruct ref_transaction *packed_transaction = NULL;\n>  \n>  \tassert(err);\n> +\t{\n> +\t\tstruct ref_update *operation =\n> +\t\t\tref_transaction_copy_or_rename_update(transaction);\n> +\n> +\t\tif (operation)\n> +\t\t\treturn files_copy_or_rename_ref(ref_store, operation,\n> +\t\t\t\t\t\t\ttransaction);\n> +\t}\n>  \n>  \tif (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n>  \t\tgoto cleanup;\n\nI know this is a construct that AI loves, but that's not following our\ncoding style.\n\n> @@ -3333,6 +3442,40 @@ static int files_transaction_finish(struct ref_store *ref_store,\n>  \n>  \n>  \tassert(err);\n> +\t{\n> +\t\tstruct ref_update *update =\n> +\t\t\tref_transaction_copy_or_rename_update(transaction);\n> +\n> +\t\tif (update) {\n> +\t\t\tstruct ref_copy_or_rename_update *operation =\n> +\t\t\t\tupdate->copy_or_rename;\n> +\t\t\tstruct files_copy_or_rename_transaction_data *data =\n> +\t\t\t\ttransaction->backend_data;\n> +\t\t\tint special_ret;\n> +\n> +\t\t\tspecial_ret = commit_ref_update(refs, data->lock, &data->orig_oid,\n> +\t\t\t\t\t\t\toperation->logmsg, 0, err);\n> +\t\t\tif (special_ret) {\n> +\t\t\t\terror(\"unable to write current sha1 into %s: %s\",\n> +\t\t\t\t      update->refname, err->buf);\n> +\t\t\t\tdata->lock = NULL;\n> +\t\t\t\tfiles_transaction_abort(ref_store, transaction, err);\n> +\t\t\t\treturn special_ret;\n> +\t\t\t} else if (data->destination_log_backed_up) {\n> +\t\t\t\tstruct strbuf path = STRBUF_INIT;\n> +\n> +\t\t\t\tfiles_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);\n> +\t\t\t\tif (unlink(path.buf) < 0 && errno != ENOENT)\n> +\t\t\t\t\twarning_errno(\"unable to remove '%s'\", path.buf);\n> +\t\t\t\tstrbuf_release(&path);\n> +\t\t\t}\n> +\t\t\tfree(data->destination_target);\n> +\t\t\tfree(data);\n> +\t\t\ttransaction->backend_data = NULL;\n> +\t\t\ttransaction->state = REF_TRANSACTION_CLOSED;\n> +\t\t\treturn special_ret;\n> +\t\t}\n> +\t}\n>  \n>  \tif (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)\n>  \t\treturn files_transaction_finish_initial(refs, transaction, err);\n\nYeah...\n\n> @@ -3476,11 +3619,105 @@ static int files_transaction_finish(struct ref_store *ref_store,\n>  \n>  static int files_transaction_abort(struct ref_store *ref_store,\n>  \t\t\t\t   struct ref_transaction *transaction,\n> -\t\t\t\t   struct strbuf *err UNUSED)\n> +\t\t\t\t   struct strbuf *err)\n>  {\n>  \tstruct files_ref_store *refs =\n>  \t\tfiles_downcast(ref_store, 0, \"ref_transaction_abort\");\n>  \n> +\t{\n> +\t\tstruct ref_update *update =\n> +\t\t\tref_transaction_copy_or_rename_update(transaction);\n\n... really?\n\nSorry, but I'm going to stop reading here. This is not in a state that\nis reviewable and has way too much stuff that is obviously generated by\nan AI without much thought being put into it by the author. I don't want\nto invest my time into a topic where the author has obviously not spent\ntheir time thinking about it, either.\n\nPatrick\n"},{"id":"553970","messageId":"CACQ=SRGSEsbNz3v3obd3JUOs2MrROnvuHkx1Dm51seCcv+12Cw@mail.gmail.com","threadId":"66350","inReplyTo":"ar-N7SA63fN_xx9P@pks.im","subject":"Re: [PATCH v2] refs: run copy and rename through transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-02T14:16:08Z","receivedAt":"2026-10-02T14:16:23Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"On Fri, Oct 2, 2026 at 12:56 PM Patrick Steinhardt <ps@pks.im> wrote:\n\n> Sorry, but what does this last sentence mean? What is the consequence\n> of it?\n\nThe intent was to address Junio's comment about making copy/rename a\nproperty of the whole ref_transaction. In v2 the extra state is\nattached to the destination ref_update instead.\n\n> why can't we make this whole mechanism completely agnostic of the\n> backend and implement this via pure transactions?\n\nThe part I was trying to preserve is the existing reflog semantics. A\nnormal ref transaction can express the logical ref updates. In example\ndeleting the old ref and creating/updating the destination. But branch\nrename/copy also moves or copies the existing reflog history. For the\nfiles backend that currently involves filesystem-level reflog\nrename/copy and D/F handling, while reftable represents the same\noperation differently. So my assumption was that the logical ref\nupdates could go through the generic transaction API, while the\nreflog-history operation would remain backend-specific.\n\n> Is this new behaviour? Is this retaining old behaviour?\n\nThe source/destination revalidation is new validation required by\nintroducing the preparing hook before the backend locks are taken. The\nhook can itself change one of the refs. Without revalidation, the hook\npayload could describe one state while the rename/copy later operates\non another state. The intention is therefore to reject an operation\nwhen the state observed by the preparing hook is no longer the state\nbeing committed.\n\n> How does all of this impact performance?\n\nEnabling reference-transaction for rename/copy naturally adds the cost\nof invoking the hook when one is installed. I measured `git branch -m`\nand `git branch -c`. Each result is the median of five blocks of 40\ncommands per version:\n\n                         Hook    Before     After     Change\nfiles     branch -m       no      5.089 ms   5.234 ms   +3.8%\nfiles     branch -m       yes    12.608 ms  11.134 ms  -10.8%\nfiles     branch -c       no      4.753 ms   4.916 ms   +3.4%\nfiles     branch -c       yes     5.509 ms  12.732 ms +137.7%\nreftable  branch -m       no      6.478 ms   6.998 ms   +7.8%\nreftable  branch -m       yes     5.918 ms  13.229 ms +114.6%\nreftable  branch -c       no      5.620 ms   6.368 ms  +10.9%\nreftable  branch -c       yes     6.261 ms  12.354 ms  +97.0%\n\n> Sorry, but I'm going to stop reading here. This is not in a state that\n> is reviewable and has way too much stuff that is obviously generated by\n> an AI without much thought being put into it by the author. I don't want\n> to invest my time into a topic where the author has obviously not spent\n> their time thinking about it, either.\n\nI'm really sorry to hear that. Yes, the patches I prepared were\nAI-assisted, but I do feel that I understand what I am doing. I would\nappreciate some understanding, though, as I do not work with C on a\ndaily basis. The bug report and my attempt to fix it came from the\nfact that I am working on a Ruby gem for per-branch and per-worktree\ncontainerization. That is why I had to write git-hooks-ext, which is\nhow I ended up running into this bug in the first place.\n\nI am not insisting that my patch should be merged. I simply thought\nthat submitting a patch might help get the bug fixed faster, and\ngetting the bug fixed is what I care about most. Karthik Nayak offered\nto help fix it, so perhaps it would be better for someone who works\nwith C on a daily basis to take it over.\n\nI can, of course, also prepare a v3, split it into more commits, and\nexplain my reasoning more clearly. But I cannot guarantee that it will\nmeet your standards, simply because I am not yet familiar with them.\n"},{"id":"554144","messageId":"asM9m-_ZoX_5UQ-I@pks.im","threadId":"66350","inReplyTo":"CACQ=SRGSEsbNz3v3obd3JUOs2MrROnvuHkx1Dm51seCcv+12Cw@mail.gmail.com","subject":"Re: [PATCH v2] refs: run copy and rename through transactions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-05T06:03:07Z","receivedAt":"2026-10-05T06:03:18Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Fri, Oct 02, 2026 at 04:16:08PM +0200, Maciej Ciemborowicz wrote:\n> On Fri, Oct 2, 2026 at 12:56 PM Patrick Steinhardt <ps@pks.im> wrote:\n> > why can't we make this whole mechanism completely agnostic of the\n> > backend and implement this via pure transactions?\n> \n> The part I was trying to preserve is the existing reflog semantics. A\n> normal ref transaction can express the logical ref updates. In example\n> deleting the old ref and creating/updating the destination. But branch\n> rename/copy also moves or copies the existing reflog history. For the\n> files backend that currently involves filesystem-level reflog\n> rename/copy and D/F handling, while reftable represents the same\n> operation differently. So my assumption was that the logical ref\n> updates could go through the generic transaction API, while the\n> reflog-history operation would remain backend-specific.\n\nYes, the reflog semantics should of course stay the same. But nowadays,\nthis would also be achievable with only backend-agnostic logic as the\nreference transactions have learned to write many reflog entries for a\nsingle reference. This was added back when we introduced the migration\nlogic to convert between two different backends.\n\nNow there's potentially two caveats:\n\n  - I don't think we have a way to delete many old reflog entries yet.\n\n  - There may be a significant impact on performance.\n\nThe question thus is whether we can avoid or fix those caveats somehow\nand thus arrive at a more future-proof mechanism.\n\n> > Is this new behaviour? Is this retaining old behaviour?\n> \n> The source/destination revalidation is new validation required by\n> introducing the preparing hook before the backend locks are taken. The\n> hook can itself change one of the refs. Without revalidation, the hook\n> payload could describe one state while the rename/copy later operates\n> on another state. The intention is therefore to reject an operation\n> when the state observed by the preparing hook is no longer the state\n> being committed.\n\nI don't feel like that's sensible. The \"preparing\" hook is explicitly\nrun before we perform locking and is documented as such. So it is fully\nexpected that the on-disk state may still change between executing this\nand the \"prepared\" phase. It is the responsibility of the hook author to\nhandle such cases, we shouldn't do this ourselves as we're now starting\nto assume semantics of the hook itself.\n\n> > Sorry, but I'm going to stop reading here. This is not in a state that\n> > is reviewable and has way too much stuff that is obviously generated by\n> > an AI without much thought being put into it by the author. I don't want\n> > to invest my time into a topic where the author has obviously not spent\n> > their time thinking about it, either.\n> \n> I'm really sorry to hear that. Yes, the patches I prepared were\n> AI-assisted, but I do feel that I understand what I am doing. I would\n> appreciate some understanding, though, as I do not work with C on a\n> daily basis. The bug report and my attempt to fix it came from the\n> fact that I am working on a Ruby gem for per-branch and per-worktree\n> containerization. That is why I had to write git-hooks-ext, which is\n> how I ended up running into this bug in the first place.\n> \n> I am not insisting that my patch should be merged. I simply thought\n> that submitting a patch might help get the bug fixed faster, and\n> getting the bug fixed is what I care about most. Karthik Nayak offered\n> to help fix it, so perhaps it would be better for someone who works\n> with C on a daily basis to take it over.\n> \n> I can, of course, also prepare a v3, split it into more commits, and\n> explain my reasoning more clearly. But I cannot guarantee that it will\n> meet your standards, simply because I am not yet familiar with them.\n\nI'd suggest to iterate then. In the current version this patch is not in\na shape that is ready for review. The patch needs to be split up, and\nthere are a lot of gaps in the commit message. Taken together that gives\nthe signal that you don't really understand what you are doing.\n\nThat doesn't mean that you cannot fix that with another iteration\nthough. But I'd suggest to take your time prepping the next iteration to\nread through the code, understand the concepts and doubt what AI spits\nout.\n\nThanks!\n\nPatrick\n"},{"id":"554401","messageId":"cover.1791395643.git.maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"20260920165037.88524-1-maciej.ciemborowicz@gmail.com","subject":"[PATCH v3 0/4] refs: run copy and rename through transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-07T18:05:13Z","receivedAt":"2026-10-07T18:05:13Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"Reference copy and rename bypass the transaction API. With files, the\nreference-transaction hook sees only the source deletion; with reftable,\nit sees neither endpoint. This series puts the logical ref updates and\nthe reflog history into ordinary transactions so hooks can observe and\nreject the complete operation.\n\nThanks to Patrick, Junio and Karthik for their feedback. Following the\nreview of v2, this version uses ref_transaction_update_reflog(), the API\nused by backend migration, to replay history. A new\nref_transaction_replace_reflog() operation supplies the missing ability\nto discard destination history before installing the queued entries.\n\nChanges since v2:\n\n* Split the work into four patches: internal hook suppression, reflog\n  replacement, copy/rename integration, and removal of the old callbacks.\n* Remove the special copy/rename dispatch from backend prepare, finish\n  and abort. Each destination points to its source update, so multiple\n  copies, renames and ordinary updates can share one transaction.\n* Keep the hook-suppression flag private and check it centrally in\n  run_transaction_hook(). It is used for the physical packed-refs child\n  transaction, whose changes the parent already reports.\n* Drop the pre-lock snapshot revalidation. A preparing hook may change\n  the source; prepared and committed report the value read under lock.\n  Copy sources are locked but are not reported as changes to the hook.\n* Stage reflog replacements during prepare. A prepared veto discards\n  staging files without restoring old values over live refs or changing\n  the source, destination or HEAD history.\n* Add coverage for complete reflog contents, mixed transactions, hook\n  vetoes, source races and failure paths, and measure the performance\n  cost of replaying history.\n\nBackend-specific work remains necessary to implement the transaction\nprimitives. Files stages logs beside logs/refs using unique temporary\nfiles. For directory/file conflicts and case-only renames, it queues\nthe destination in packed-refs: the loose source cannot remain visible\nwhile also making room for a loose destination lock. During finish, a\nbackup preserves the source log until the destination log is installed;\nan installation error restores that backup. Reftable writes tombstones\nfor old destination entries in the same table as the replacement.\n\nThe backends' final reflog records remain distinct: files appends\nold->old; reftable appends old->zero and zero->old for rename, or\nzero->old for copy. Forced reftable copies do change behavior: unrelated\ndestination history is replaced by source history, matching files when\nthe source has a reflog.\n\nReplaying history requires O(N) time and memory. Buffered staging avoids\na write system call per entry, but files rename loses its constant-time\nreflog move. On macOS/arm64, with no hook configured, median milliseconds\nper operation over seven samples of ten operations were:\n\n                    entries       base        v3\n  files copy             10       5.05      5.08\n  files rename           10       4.88      5.26\n  files copy          10000      13.20     16.11\n  files rename        10000       5.09     17.09\n  reftable copy          10       5.25      6.11\n  reftable rename        10       5.81      6.50\n  reftable copy       10000      53.39     68.06\n  reftable rename     10000      43.44     54.06\n\nProcess startup is included. Copy overwrites the same destination;\nrename alternates between two names. Reftable starts from a migration\nof the same files fixture. Repeated operations add normal log entries\nand incur normal reftable compaction. Patch 3 adds\nt/perf/p1424-ref-copy-rename.sh; these measurements used a separate\nmonotonic-clock driver because GNU time is not installed here. The\nfiles rename regression is a cost of this design, not just hook overhead.\n\nValidation covered 15 relevant suites with files and reftable, including\nbranch, update-ref, hooks, migration, reflogs and worktree refs. The two\nnew suites contain 34 tests, with backend-specific skips; they also\npassed with SHA-256 and AddressSanitizer/UndefinedBehaviorSanitizer.\nThe reflog prerequisite and main change were tested as intermediate\ntrees. This was not a full test-suite run or a Linux/Windows run.\n\nFiles transactions are still not crash-atomic. A later failure in\nfinish may leave partial ref changes, as with ordinary transactions.\nThe source-log backup covers an installation error, not a process crash.\nUnrelated nested renames can also contend for the global packed-refs\nlock.\n\nThe base is 0f8e75abeb. This series is independent of the separate\nbatched-deletion old-OID fix discussed elsewhere in the thread.\n\nOriginal patch:\nhttps://lore.kernel.org/git/20260920165037.88524-1-maciej.ciemborowicz@gmail.com/\n\nThe range-diff below treats the rewritten and split implementation as\nfour new commits, so the changes above describe the mapping from v2.\n\nMaciej Ciemborowicz (4):\n  refs: distinguish internal transactions from logical updates\n  refs: support replacing reflogs in a transaction\n  refs: run copy and rename through ordinary transactions\n  refs: remove backend-specific copy and rename callbacks\n\n Documentation/githooks.adoc     |  10 +\n refs.c                          | 200 ++++++++-\n refs.h                          |  25 ++\n refs/debug.c                    |  24 --\n refs/files-backend.c            | 739 +++++++++++++++++---------------\n refs/packed-backend.c           |   2 -\n refs/refs-internal.h            |  28 +-\n refs/reftable-backend.c         | 334 ++-------------\n t/helper/test-ref-store.c       |  76 ++++\n t/perf/p1424-ref-copy-rename.sh |  48 +++\n t/t1424-ref-copy-transaction.sh | 352 +++++++++++++++\n t/t1425-reflog-transaction.sh   |  59 +++\n 12 files changed, 1206 insertions(+), 691 deletions(-)\n create mode 100755 t/perf/p1424-ref-copy-rename.sh\n create mode 100755 t/t1424-ref-copy-transaction.sh\n create mode 100755 t/t1425-reflog-transaction.sh\n\nRange-diff against v2:\n1:  d852537d8c < -:  ---------- refs: run copy and rename through transactions\n-:  ---------- > 1:  6d7c146e57 refs: distinguish internal transactions from logical updates\n-:  ---------- > 2:  5c3ec4eb49 refs: support replacing reflogs in a transaction\n-:  ---------- > 3:  77af4e809c refs: run copy and rename through ordinary transactions\n-:  ---------- > 4:  83fa644fb3 refs: remove backend-specific copy and rename callbacks\n\nbase-commit: 0f8e75abebff0877cae681a3d5ff31ac47f54220\n-- \n2.39.3 (Apple Git-146)\n\n"},{"id":"554402","messageId":"6d7c146e57b3e626a0469f2289d375652ed8fec1.1791395643.git.maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"cover.1791395643.git.maciej.ciemborowicz@gmail.com","subject":"[PATCH v3 1/4] refs: distinguish internal transactions from logical updates","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-07T18:05:14Z","receivedAt":"2026-10-07T18:05:14Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"A ref backend may use a nested transaction to persist part of a logical\nupdate. Provide an internal-only transaction flag so that these physical\nupdates can avoid reporting the same operation to reference-transaction\nhooks again.\n\nKeep the check in run_transaction_hook(), covering every hook state in\none place. The flag is deliberately not part of the public refs API.\nSubsequent changes use it for packed-refs updates belonging to a copy or\nrename.\n\nSigned-off-by: Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com>\n---\n refs.c               | 3 +++\n refs/refs-internal.h | 3 +++\n 2 files changed, 6 insertions(+)\n\ndiff --git a/refs.c b/refs.c\nindex 92d5df5b71..00b1b51280 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2666,6 +2666,9 @@ static int run_transaction_hook(struct ref_transaction *transaction,\n \tstruct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;\n \tint ret = 0;\n \n+\tif (transaction->flags & REF_TRANSACTION_FLAG_INTERNAL)\n+\t\treturn 0;\n+\n \tstrvec_push(&opt.args, state);\n \n \topt.feed_pipe = transaction_hook_feed_stdin;\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex c3ac7b556f..0b41f5fa4b 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -8,6 +8,9 @@\n struct fsck_options;\n struct ref_transaction;\n \n+/* Physical updates nested in a transaction already reported to hooks. */\n+#define REF_TRANSACTION_FLAG_INTERNAL (1 << 2)\n+\n /*\n  * Data structures and functions for the internal use of the refs\n  * module. Code outside of the refs module should use only the public\n-- \n2.39.3 (Apple Git-146)\n\n\n"},{"id":"554403","messageId":"5c3ec4eb493e300b74418ef0f3e35c7820abf399.1791395643.git.maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"cover.1791395643.git.maciej.ciemborowicz@gmail.com","subject":"[PATCH v3 2/4] refs: support replacing reflogs in a transaction","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-07T18:05:15Z","receivedAt":"2026-10-07T18:05:15Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"Explicit log-only updates can already recreate a reflog during\nmigration. Add ref_transaction_replace_reflog() to replace existing\nhistory with these entries without changing the reference itself. Keep\nits marker flag private to the refs implementation so callers cannot\nattach this cross-update behavior to an ordinary ref update\naccidentally.\n\nStage files-backend replacements in unique temporary files beside the\nlogs and install them only during finish. Buffer writes and order\nentries by their requested index. Aborting prepare only removes staging\nfiles. In reftable, tombstone the previous entries in the same table as\nthe replacement.\n\nDo not treat a log-only entry with a null new OID as a reference\ndeletion: it is part of the history being replayed. Propagate read\nerrors instead of silently copying a partial log.\n\nTest preservation of null-OID entries, ordering, removal, hook rejection\nand duplicate replacement. This is preparation for copying and renaming\nthrough the ordinary transaction path.\n\nSigned-off-by: Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com>\n---\n refs.c                        |  34 ++++++-\n refs.h                        |   9 ++\n refs/files-backend.c          | 180 +++++++++++++++++++++++++++++++---\n refs/refs-internal.h          |   3 +\n refs/reftable-backend.c       |  10 +-\n t/helper/test-ref-store.c     |  40 ++++++++\n t/t1425-reflog-transaction.sh |  59 +++++++++++\n 7 files changed, 320 insertions(+), 15 deletions(-)\n create mode 100755 t/t1425-reflog-transaction.sh\n\ndiff --git a/refs.c b/refs.c\nindex 00b1b51280..bf219573cd 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1413,7 +1413,6 @@ enum ref_transaction_error ref_transaction_update(struct ref_transaction *transa\n \t\tstrbuf_addstr(err, _(\"refusing to force and skip creation of reflog\"));\n \t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \t}\n-\n \tif (!transaction_refname_valid(refname, new_oid, flags, err))\n \t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \n@@ -1496,6 +1495,39 @@ int ref_transaction_update_reflog(struct ref_transaction *transaction,\n \treturn 0;\n }\n \n+int ref_transaction_replace_reflog(struct ref_transaction *transaction,\n+\t\t\t\t    const char *refname,\n+\t\t\t\t    struct strbuf *err)\n+{\n+\tsize_t i;\n+\tunsigned int flags = REF_LOG_ONLY | REF_NO_DEREF |\n+\t\tREF_SKIP_CREATE_REFLOG | REF_REPLACE_REFLOG;\n+\n+\tassert(err);\n+\tif (transaction->flags &\n+\t    (REF_TRANSACTION_FLAG_INITIAL | REF_TRANSACTION_ALLOW_FAILURE)) {\n+\t\tstrbuf_addstr(err, _(\"reflog replacement requires an \"\n+\t\t\t\t     \"all-or-nothing, non-initial transaction\"));\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n+\t}\n+\tif (!transaction_refname_valid(refname, NULL, flags, err))\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\n+\t\tif ((update->flags & REF_REPLACE_REFLOG) &&\n+\t\t    !strcmp(update->refname, refname)) {\n+\t\t\tstrbuf_addf(err, _(\"reflog replacement for '%s' already queued\"),\n+\t\t\t\t    refname);\n+\t\t\treturn REF_TRANSACTION_ERROR_NAME_CONFLICT;\n+\t\t}\n+\t}\n+\n+\tref_transaction_add_update(transaction, refname, flags,\n+\t\t\t\t   NULL, NULL, NULL, NULL, NULL, NULL, NULL);\n+\treturn 0;\n+}\n+\n int ref_transaction_create(struct ref_transaction *transaction,\n \t\t\t   const char *refname,\n \t\t\t   const struct object_id *new_oid,\ndiff --git a/refs.h b/refs.h\nindex 9979446d15..7e80d0bdfb 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -939,6 +939,15 @@ int ref_transaction_update_reflog(struct ref_transaction *transaction,\n \t\t\t\t  uint64_t index,\n \t\t\t\t  struct strbuf *err);\n \n+/*\n+ * Replace a reference's reflog with the explicit entries added to the same\n+ * transaction via ref_transaction_update_reflog(). Without such entries,\n+ * remove the reflog. This operation does not change the reference itself.\n+ */\n+int ref_transaction_replace_reflog(struct ref_transaction *transaction,\n+\t\t\t\t    const char *refname,\n+\t\t\t\t    struct strbuf *err);\n+\n /*\n  * Add a reference creation to transaction. new_oid is the value that\n  * the reference should have after the update; it must not be\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 71628550f2..70d1ff80c4 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1983,6 +1983,22 @@ static int files_create_reflog(struct ref_store *ref_store, const char *refname,\n \treturn 0;\n }\n \n+static void format_reflog_entry(struct strbuf *sb,\n+\t\t\t\tconst struct object_id *old_oid,\n+\t\t\t\tconst struct object_id *new_oid,\n+\t\t\t\tconst char *committer, const char *msg)\n+{\n+\tif (!committer)\n+\t\tcommitter = git_committer_info(0);\n+\n+\tstrbuf_addf(sb, \"%s %s %s\", oid_to_hex(old_oid), oid_to_hex(new_oid), committer);\n+\tif (msg && *msg) {\n+\t\tstrbuf_addch(sb, '\\t');\n+\t\tstrbuf_addstr(sb, msg);\n+\t}\n+\tstrbuf_addch(sb, '\\n');\n+}\n+\n static int log_ref_write_fd(int fd, const struct object_id *old_oid,\n \t\t\t    const struct object_id *new_oid,\n \t\t\t    const char *committer, const char *msg)\n@@ -1990,15 +2006,7 @@ static int log_ref_write_fd(int fd, const struct object_id *old_oid,\n \tstruct strbuf sb = STRBUF_INIT;\n \tint ret = 0;\n \n-\tif (!committer)\n-\t\tcommitter = git_committer_info(0);\n-\n-\tstrbuf_addf(&sb, \"%s %s %s\", oid_to_hex(old_oid), oid_to_hex(new_oid), committer);\n-\tif (msg && *msg) {\n-\t\tstrbuf_addch(&sb, '\\t');\n-\t\tstrbuf_addstr(&sb, msg);\n-\t}\n-\tstrbuf_addch(&sb, '\\n');\n+\tformat_reflog_entry(&sb, old_oid, new_oid, committer, msg);\n \tif (write_in_full(fd, sb.buf, sb.len) < 0)\n \t\tret = -1;\n \tstrbuf_release(&sb);\n@@ -2397,6 +2405,8 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store,\n \n \twhile (!ret && !strbuf_getwholeline(&sb, logfp, '\\n'))\n \t\tret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);\n+\tif (ferror(logfp))\n+\t\tret = -1;\n \tfclose(logfp);\n \tstrbuf_release(&sb);\n \treturn ret;\n@@ -2665,12 +2675,120 @@ static enum ref_transaction_error check_old_oid(struct ref_update *update,\n \treturn REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;\n }\n \n+struct staged_reflog {\n+\tstruct tempfile *file;\n+\tstruct ref_update **updates;\n+\tsize_t nr, alloc;\n+};\n+\n struct files_transaction_backend_data {\n \tstruct ref_transaction *packed_transaction;\n \tint packed_refs_locked;\n \tstruct strmap ref_locks;\n+\tstruct strmap reflog_files;\n };\n \n+static int reflog_update_cmp(const void *a, const void *b)\n+{\n+\tconst struct ref_update *one = *(struct ref_update * const *)a;\n+\tconst struct ref_update *two = *(struct ref_update * const *)b;\n+\n+\treturn (one->index > two->index) - (one->index < two->index);\n+}\n+\n+static int prepare_reflog_replacements(struct files_ref_store *refs,\n+\t\t\t\t      struct ref_transaction *transaction,\n+\t\t\t\t      struct strbuf *err)\n+{\n+\tstruct files_transaction_backend_data *data = transaction->backend_data;\n+\tstruct strbuf path = STRBUF_INIT;\n+\tstruct strbuf contents = STRBUF_INIT;\n+\tstruct hashmap_iter iter;\n+\tstruct strmap_entry *entry;\n+\tsize_t i;\n+\tint ret = -1;\n+\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\t\tstruct staged_reflog *log;\n+\n+\t\tif (!(update->flags & REF_REPLACE_REFLOG))\n+\t\t\tcontinue;\n+\t\tCALLOC_ARRAY(log, 1);\n+\t\tstrmap_put(&data->reflog_files, update->refname, log);\n+\t}\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\t\tstruct staged_reflog *log = strmap_get(&data->reflog_files, update->refname);\n+\n+\t\tif (!log || !(update->flags & REF_LOG_ONLY) ||\n+\t\t    !(update->flags & REF_HAVE_NEW))\n+\t\t\tcontinue;\n+\t\tif (!(update->flags & REF_LOG_USE_PROVIDED_OIDS)) {\n+\t\t\tstrbuf_addf(err, \"replacing reflog '%s' requires explicit OIDs\",\n+\t\t\t\t    update->refname);\n+\t\t\tgoto out;\n+\t\t}\n+\t\tALLOC_GROW(log->updates, log->nr + 1, log->alloc);\n+\t\tlog->updates[log->nr++] = update;\n+\t}\n+\tstrmap_for_each_entry(&data->reflog_files, &iter, entry) {\n+\t\tstruct staged_reflog *log = entry->value;\n+\t\tint fd;\n+\n+\t\tif (!log->nr)\n+\t\t\tcontinue;\n+\t\tQSORT(log->updates, log->nr, reflog_update_cmp);\n+\t\t/* Keep the staging file beside the logs, which may be on another device. */\n+\t\tstrbuf_reset(&path);\n+\t\tfiles_reflog_path(refs, &path, is_root_ref(entry->key) ?\n+\t\t\t\t \".tmp-reflog-XXXXXX\" : \"refs/.tmp-reflog-XXXXXX\");\n+\t\tif (safe_create_leading_directories(refs->base.repo, path.buf))\n+\t\t\tgoto write_error;\n+\t\tlog->file = mks_tempfile_m(path.buf, 0666);\n+\t\tif (!log->file)\n+\t\t\tgoto write_error;\n+\t\tfd = get_tempfile_fd(log->file);\n+\t\tfor (i = 0; i < log->nr; i++) {\n+\t\t\tstruct ref_update *update = log->updates[i];\n+\n+\t\t\tformat_reflog_entry(&contents, &update->old_oid, &update->new_oid,\n+\t\t\t\t\t    update->committer_info, update->msg);\n+\t\t\tif (contents.len >= 65536) {\n+\t\t\t\tif (write_in_full(fd, contents.buf, contents.len) < 0)\n+\t\t\t\t\tgoto write_error;\n+\t\t\t\tstrbuf_reset(&contents);\n+\t\t\t}\n+\t\t}\n+\t\tif (write_in_full(fd, contents.buf, contents.len) < 0)\n+\t\t\tgoto write_error;\n+\t\tstrbuf_reset(&contents);\n+\t\tif (adjust_shared_perm(refs->base.repo, get_tempfile_path(log->file)) ||\n+\t\t    close_tempfile_gently(log->file))\n+\t\t\tgoto write_error;\n+\t}\n+\tret = 0;\n+\tgoto out;\n+\n+write_error:\n+\tstrbuf_addf(err, \"cannot write staged reflog: %s\", strerror(errno));\n+out:\n+\tstrbuf_release(&path);\n+\tstrbuf_release(&contents);\n+\treturn ret;\n+}\n+\n+static int install_reflog(const char *path, void *data)\n+{\n+\tstruct staged_reflog *log = data;\n+\tint ret = rename(get_tempfile_path(log->file), path);\n+\n+\t/* Some systems report ENOTDIR when the destination is a directory. */\n+\tif (ret && errno == ENOTDIR)\n+\t\terrno = EISDIR;\n+\treturn ret;\n+}\n+\n /*\n  * Prepare for carrying out update:\n  * - Lock the reference referred to by update.\n@@ -2929,6 +3047,17 @@ static void files_transaction_cleanup(struct files_ref_store *refs,\n \t}\n \n \tif (backend_data) {\n+\t\tstruct hashmap_iter iter;\n+\t\tstruct strmap_entry *entry;\n+\n+\t\tstrmap_for_each_entry(&backend_data->reflog_files, &iter, entry) {\n+\t\t\tstruct staged_reflog *log = entry->value;\n+\n+\t\t\tdelete_tempfile(&log->file);\n+\t\t\tfree(log->updates);\n+\t\t\tfree(log);\n+\t\t}\n+\t\tstrmap_clear(&backend_data->reflog_files, 0);\n \t\tif (backend_data->packed_transaction &&\n \t\t    ref_transaction_abort(backend_data->packed_transaction, &err)) {\n \t\t\terror(\"error aborting transaction: %s\", err.buf);\n@@ -2970,6 +3099,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \n \tCALLOC_ARRAY(backend_data, 1);\n \tstrmap_init(&backend_data->ref_locks);\n+\tstrmap_init(&backend_data->reflog_files);\n \ttransaction->backend_data = backend_data;\n \n \t/*\n@@ -3120,6 +3250,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\t}\n \t}\n \n+\tret = prepare_reflog_replacements(refs, transaction, err);\n+\n cleanup:\n \tfree(head_ref);\n \tstring_list_clear(&refnames_to_check, 1);\n@@ -3344,6 +3476,31 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \tbackend_data = transaction->backend_data;\n \tpacked_transaction = backend_data->packed_transaction;\n \n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\t\tstruct staged_reflog *log;\n+\n+\t\tif (!(update->flags & REF_REPLACE_REFLOG))\n+\t\t\tcontinue;\n+\t\tlog = strmap_get(&backend_data->reflog_files, update->refname);\n+\t\tstrbuf_reset(&sb);\n+\t\tfiles_reflog_path(refs, &sb, update->refname);\n+\t\tif (!log->file) {\n+\t\t\tif (unlink(sb.buf) && errno != ENOENT && errno != EISDIR)\n+\t\t\t\tret = -1;\n+\t\t} else if (raceproof_create_file(refs, sb.buf, install_reflog, log)) {\n+\t\t\tret = -1;\n+\t\t} else {\n+\t\t\tdelete_tempfile(&log->file);\n+\t\t}\n+\t\tif (ret) {\n+\t\t\tstrbuf_addf(err, \"cannot replace reflog '%s': %s\",\n+\t\t\t\t    update->refname, strerror(errno));\n+\t\t}\n+\t\tif (ret)\n+\t\t\tgoto cleanup;\n+\t}\n+\n \t/* Perform updates first so live commits remain referenced */\n \tfor (i = 0; i < transaction->nr; i++) {\n \t\tstruct ref_update *update = transaction->updates[i];\n@@ -3352,8 +3509,9 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \t\tif (update->rejection_err)\n \t\t\tcontinue;\n \n-\t\tif (update->flags & REF_NEEDS_COMMIT ||\n-\t\t    update->flags & REF_LOG_ONLY) {\n+\t\tif ((update->flags & REF_NEEDS_COMMIT ||\n+\t\t     update->flags & REF_LOG_ONLY) &&\n+\t\t    !strmap_contains(&backend_data->reflog_files, update->refname)) {\n \t\t\tif (parse_and_write_reflog(refs, update, lock, err)) {\n \t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\t\tgoto cleanup;\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 0b41f5fa4b..19fdf39d1d 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -49,6 +49,9 @@ struct ref_transaction;\n  */\n #define REF_HAVE_PEELED (1 << 15)\n \n+/* Replace a reflog with the explicit log-only updates in the transaction. */\n+#define REF_REPLACE_REFLOG (1 << 16)\n+\n /*\n  * Return the length of time to retry acquiring a loose reference lock\n  * before giving up, in milliseconds:\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 10db03991e..203b111f3d 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1503,9 +1503,11 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data\n \t\t * - `core.logAllRefUpdates` tells us to create the reflog for\n \t\t *   the given ref.\n \t\t */\n-\t\tif ((u->flags & REF_HAVE_NEW) &&\n+\t\tif ((u->flags & REF_REPLACE_REFLOG) ||\n+\t\t    ((u->flags & REF_HAVE_NEW) &&\n+\t\t    !(u->flags & REF_LOG_ONLY) &&\n \t\t    !(u->type & REF_ISSYMREF) &&\n-\t\t    ref_update_has_null_new_value(u)) {\n+\t\t    ref_update_has_null_new_value(u))) {\n \t\t\tstruct reftable_log_record log = {0};\n \t\t\tstruct reftable_iterator it = {0};\n \n@@ -1548,8 +1550,10 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data\n \n \t\t\tif (ret)\n \t\t\t\tgoto done;\n-\t\t} else if (!(u->flags & REF_SKIP_CREATE_REFLOG) &&\n+\t\t}\n+\t\tif (!(u->flags & REF_SKIP_CREATE_REFLOG) &&\n \t\t\t   (u->flags & REF_HAVE_NEW) &&\n+\t\t\t   (!ref_update_has_null_new_value(u) || (u->flags & REF_LOG_ONLY)) &&\n \t\t\t   (u->flags & REF_FORCE_CREATE_REFLOG ||\n \t\t\t    should_write_log(arg->refs, u->refname))) {\n \t\t\tstruct reftable_log_record *log;\ndiff --git a/t/helper/test-ref-store.c b/t/helper/test-ref-store.c\nindex db58f00589..7ce10c28af 100644\n--- a/t/helper/test-ref-store.c\n+++ b/t/helper/test-ref-store.c\n@@ -146,6 +146,45 @@ static int cmd_rename_ref(struct ref_store *refs, const char **argv)\n \treturn refs_rename_ref(refs, oldref, newref, logmsg);\n }\n \n+static int cmd_reflog_transaction(struct ref_store *refs, const char **argv)\n+{\n+\tconst char *refname = notnull(*argv++, \"refname\");\n+\tconst char *mode = notnull(*argv++, \"mode\");\n+\tstruct strbuf err = STRBUF_INIT;\n+\tstruct ref_transaction *transaction = ref_store_transaction_begin(refs, 0, &err);\n+\tint ret = !transaction;\n+\n+\tif (!ret) {\n+\t\tif (!strcmp(mode, \"replace\")) {\n+\t\t\tret = ref_transaction_replace_reflog(transaction, refname, &err);\n+\t\t} else if (!strcmp(mode, \"replace-twice\")) {\n+\t\t\tret = ref_transaction_replace_reflog(transaction, refname, &err) ||\n+\t\t\t\tref_transaction_replace_reflog(transaction, refname, &err);\n+\t\t} else if (strcmp(mode, \"append\")) {\n+\t\t\tdie(\"unknown reflog transaction mode: %s\", mode);\n+\t\t}\n+\t}\n+\twhile (!ret && *argv) {\n+\t\tuint64_t index = strtoumax(notnull(*argv++, \"index\"), NULL, 10);\n+\t\tstruct object_id old_oid, new_oid;\n+\t\tconst char *message;\n+\n+\t\tif (get_oid_hex(notnull(*argv++, \"old-oid\"), &old_oid) ||\n+\t\t    get_oid_hex(notnull(*argv++, \"new-oid\"), &new_oid))\n+\t\t\tdie(\"invalid object ID\");\n+\t\tmessage = notnull(*argv++, \"message\");\n+\t\tret = ref_transaction_update_reflog(transaction, refname,\n+\t\t\t\t&new_oid, &old_oid, NULL, message, index, &err);\n+\t}\n+\tif (!ret)\n+\t\tret = ref_transaction_commit(transaction, &err);\n+\tif (ret)\n+\t\terror(\"%s\", err.buf);\n+\tref_transaction_free(transaction);\n+\tstrbuf_release(&err);\n+\treturn ret;\n+}\n+\n static int each_ref(const struct reference *ref, void *cb_data UNUSED)\n {\n \tprintf(\"%s %s 0x%x\\n\", oid_to_hex(ref->oid), ref->name, ref->flags);\n@@ -308,6 +347,7 @@ static struct command commands[] = {\n \t{ \"create-symref\", cmd_create_symref },\n \t{ \"delete-refs\", cmd_delete_refs },\n \t{ \"rename-ref\", cmd_rename_ref },\n+\t{ \"reflog-transaction\", cmd_reflog_transaction },\n \t{ \"for-each-ref\", cmd_for_each_ref },\n \t{ \"for-each-ref--exclude\", cmd_for_each_ref__exclude },\n \t{ \"resolve-ref\", cmd_resolve_ref },\ndiff --git a/t/t1425-reflog-transaction.sh b/t/t1425-reflog-transaction.sh\nnew file mode 100755\nindex 0000000000..ee5278045e\n--- /dev/null\n+++ b/t/t1425-reflog-transaction.sh\n@@ -0,0 +1,59 @@\n+#!/bin/sh\n+\n+test_description='explicit reflog entries in reference transactions'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit A &&\n+\ttest_commit B &&\n+\tA=$(git rev-parse A) &&\n+\tB=$(git rev-parse B)\n+'\n+\n+test_expect_success 'log-only entry with null new OID does not delete existing history' '\n+\tgit branch source \"$A\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/source >before &&\n+\ttest-tool ref-store main reflog-transaction refs/heads/source append \\\n+\t\t1 \"$A\" \"$ZERO_OID\" deletion &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/source >after &&\n+\tsed \"$ d\" after >history &&\n+\ttest_cmp before history &&\n+\ttest_grep \"$A $ZERO_OID .*deletion\" after &&\n+\ttest_cmp_rev \"$A\" source\n+'\n+\n+test_expect_success 'replace reflog with entries ordered by index' '\n+\ttest-tool ref-store main reflog-transaction refs/heads/source replace \\\n+\t\t2 \"$A\" \"$B\" second \\\n+\t\t1 \"$ZERO_OID\" \"$A\" first &&\n+\tgit reflog show --format=%gs source >actual &&\n+\tprintf \"%s\\n\" second first >expect &&\n+\ttest_cmp expect actual &&\n+\ttest_cmp_rev \"$A\" source\n+'\n+\n+test_expect_success 'prepared hook can reject replacement without losing history' '\n+\ttest_when_finished \"rm -f .git/hooks/reference-transaction\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/source >before &&\n+\twrite_script .git/hooks/reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\ttest_must_fail test-tool ref-store main reflog-transaction refs/heads/source replace \\\n+\t\t1 \"$ZERO_OID\" \"$B\" replacement &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/source >after &&\n+\ttest_cmp before after\n+'\n+\n+test_expect_success 'replacement with no entries removes only the reflog' '\n+\ttest-tool ref-store main reflog-transaction refs/heads/source replace &&\n+\ttest_must_fail git reflog exists refs/heads/source &&\n+\ttest_cmp_rev \"$A\" source\n+'\n+\n+test_expect_success 'duplicate replacement is rejected' '\n+\ttest_must_fail test-tool ref-store main reflog-transaction \\\n+\t\trefs/heads/source replace-twice\n+'\n+\n+test_done\n-- \n2.39.3 (Apple Git-146)\n\n\n"},{"id":"554404","messageId":"77af4e809c8de73117c76722b48184d4e9efbd0c.1791395643.git.maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"cover.1791395643.git.maciej.ciemborowicz@gmail.com","subject":"[PATCH v3 3/4] refs: run copy and rename through ordinary transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-07T18:05:16Z","receivedAt":"2026-10-07T18:05:16Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"Copy and rename currently bypass reference transactions. A hook sees the\nsource deletion with files and sees neither endpoint with reftable.\nQueue the source and destination as ordinary updates instead, with the\ndestination referring to its source update.\n\nRead and lock the source during prepare, then replay its history through\nthe explicit reflog API. Keep this state per update so multiple\nindependent operations and ordinary updates can share a transaction. Do\nnot reject source changes made before locking: preparing is advisory,\nwhile prepared sees the values read under lock and unchanged on-disk\nrefs and reflogs.\n\nFor files D/F and case-only renames, the source prevents locking a loose\ndestination. Queue the destination in packed-refs, protected by the\nsource lock (and an ancestor lock when needed). Suppress hooks for this\nphysical child transaction only. Keep source logs until finish and use\nthe staged replacements so a veto needs no rollback writes. During\nfinish, retain a unique backup of a D/F source log until its destination\nlog is installed, and restore it if installation fails.\n\nPreserve the backends' distinct final reflog records. Forced reftable\ncopies now replace destination history instead of retaining unrelated\ndestination entries. Add regression tests for hooks, full histories,\nHEAD, source races, mixed transactions, lock and installation errors,\nrestoration and cleanup, and a reproducible performance test.\n\nReplaying history costs O(N) time and memory. In a local no-hook\nbenchmark with 10,000 entries, files rename takes about 17 ms versus 5\nms before, and reftable rename about 54 ms versus 44 ms. Short-history\noperations remain within roughly 1 ms. This trades the files\nconstant-time rename for staging that does not expose partial changes to\na prepared hook.\n\nHelped-by: Karthik Nayak <karthik.188@gmail.com>\nHelped-by: Junio C Hamano <gitster@pobox.com>\nHelped-by: Patrick Steinhardt <ps@pks.im>\nSigned-off-by: Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com>\n---\n Documentation/githooks.adoc     |  10 +\n refs.c                          | 163 +++++++++++++--\n refs.h                          |  16 ++\n refs/files-backend.c            | 225 +++++++++++++++++++-\n refs/refs-internal.h            |  13 ++\n refs/reftable-backend.c         |  37 +++-\n t/helper/test-ref-store.c       |  36 ++++\n t/perf/p1424-ref-copy-rename.sh |  48 +++++\n t/t1424-ref-copy-transaction.sh | 352 ++++++++++++++++++++++++++++++++\n 9 files changed, 880 insertions(+), 20 deletions(-)\n create mode 100755 t/perf/p1424-ref-copy-rename.sh\n create mode 100755 t/t1424-ref-copy-transaction.sh\n\ndiff --git a/Documentation/githooks.adoc b/Documentation/githooks.adoc\nindex ed045940d1..629dd3e4d1 100644\n--- a/Documentation/githooks.adoc\n+++ b/Documentation/githooks.adoc\n@@ -518,6 +518,16 @@ distinguish these cases, you can inspect the current value of\n references are not resolved: `<ref-name>` will reflect the symbolic reference\n itself rather than the object it points to.\n \n+Copying or renaming a reference is reported as one transaction. A rename\n+reports the deletion of the source and the update of the destination;\n+a copy reports only the destination update. Copying the reflog does not\n+produce additional hook input. In the \"preparing\" state, the old values\n+are unspecified (all zeroes), and the destination's new value is the\n+source value read before locking. In subsequent states, these operations\n+report the old values and the source value read under lock. Thus, a\n+\"preparing\" hook may change the source before it is copied or renamed.\n+A \"prepared\" hook still sees the original references and reflogs on disk.\n+\n For symbolic reference updates the `<old_value>` and `<new-value>`\n fields could denote references instead of objects. A reference will be\n denoted with a 'ref:' prefix, like `ref:<ref-target>`.\ndiff --git a/refs.c b/refs.c\nindex bf219573cd..eb3e58c789 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3158,28 +3158,165 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n \treturn ret;\n }\n \n+static int transaction_copy_ref(struct ref_transaction *transaction,\n+\t\t\t\tconst char *oldref, const char *newref,\n+\t\t\t\tunsigned int source_flags, const char *logmsg,\n+\t\t\t\tstruct strbuf *err)\n+{\n+\tstruct ref_update *source, *destination;\n+\tstruct object_id oid;\n+\tint type;\n+\n+\tif (transaction->state != REF_TRANSACTION_OPEN)\n+\t\tBUG(\"copy added to a prepared transaction\");\n+\tif (!refs_resolve_ref_unsafe(transaction->ref_store, oldref,\n+\t\t\tRESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, &oid, &type)) {\n+\t\tstrbuf_addf(err, _(\"refname %s not found\"), oldref);\n+\t\treturn -1;\n+\t}\n+\tif (type & REF_ISSYMREF) {\n+\t\tstrbuf_addf(err, _(\"refname %s is a symbolic ref, copying or renaming it is not supported\"), oldref);\n+\t\treturn -1;\n+\t}\n+\tif (!strcmp(oldref, newref))\n+\t\treturn 0;\n+\tif (!transaction_refname_valid(newref, &oid, REF_NO_DEREF, err))\n+\t\treturn -1;\n+\tif (transaction->flags & (REF_TRANSACTION_FLAG_INITIAL | REF_TRANSACTION_ALLOW_FAILURE)) {\n+\t\tstrbuf_addstr(err, _(\"copy and rename require an all-or-nothing, non-initial transaction\"));\n+\t\treturn -1;\n+\t}\n+\n+\t/* This value describes the request to 'preparing'; locks decide the value used. */\n+\tsource = ref_transaction_add_update(transaction, oldref,\n+\t\t\tREF_NO_DEREF | REF_COPY_SOURCE | source_flags,\n+\t\t\t(source_flags & REF_HAVE_NEW) ? null_oid(transaction->ref_store->repo->hash_algo) : NULL,\n+\t\t\tNULL, NULL, NULL, NULL, NULL, logmsg);\n+\tdestination = ref_transaction_add_update(transaction, newref,\n+\t\t\tREF_NO_DEREF | REF_HAVE_NEW,\n+\t\t\t&oid, NULL, NULL, NULL, NULL, NULL, logmsg);\n+\tdestination->copy_from = source;\n+\treturn ref_transaction_replace_reflog(transaction, newref, err);\n+}\n+\n+int ref_transaction_copy(struct ref_transaction *transaction,\n+\t\t\t const char *oldref, const char *newref,\n+\t\t\t const char *logmsg, struct strbuf *err)\n+{\n+\treturn transaction_copy_ref(transaction, oldref, newref,\n+\t\t\t\t    REF_LOG_ONLY | REF_SKIP_CREATE_REFLOG, logmsg, err);\n+}\n+\n+int ref_transaction_rename(struct ref_transaction *transaction,\n+\t\t\t   const char *oldref, const char *newref,\n+\t\t\t   const char *logmsg, struct strbuf *err)\n+{\n+\treturn transaction_copy_ref(transaction, oldref, newref,\n+\t\t\t\t    REF_HAVE_NEW, logmsg, err);\n+}\n+\n+int ref_update_record_copy_source(struct ref_update *update,\n+\t\t\t\t  const struct object_id *oid,\n+\t\t\t\t  struct strbuf *err)\n+{\n+\tif ((update->type & REF_ISSYMREF) || is_null_oid(oid)) {\n+\t\tstrbuf_addf(err, _(\"'%s' is not an existing direct reference\"), update->refname);\n+\t\treturn -1;\n+\t}\n+\toidcpy(&update->old_oid, oid);\n+\tupdate->flags |= REF_HAVE_OLD;\n+\treturn 0;\n+}\n+\n+struct copy_reflog_data {\n+\tstruct ref_transaction *transaction;\n+\tconst char *refname;\n+\tstruct strbuf *err;\n+};\n+\n+static int copy_reflog_entry(const char *refname UNUSED,\n+\t\t\t     struct object_id *old_oid, struct object_id *new_oid,\n+\t\t\t     const char *committer, timestamp_t timestamp, int tz,\n+\t\t\t     const char *message, void *cb_data)\n+{\n+\tstruct copy_reflog_data *data = cb_data;\n+\tstruct strbuf ident = STRBUF_INIT;\n+\tint ret;\n+\n+\tstrbuf_addf(&ident, \"%s %\" PRItime \" %+05d\", committer, timestamp, tz);\n+\tret = ref_transaction_update_reflog(data->transaction, data->refname,\n+\t\t\tnew_oid, old_oid, ident.buf, message,\n+\t\t\tdata->transaction->max_index + 1, data->err);\n+\tstrbuf_release(&ident);\n+\treturn ret;\n+}\n+\n+int ref_transaction_prepare_copy(struct ref_transaction *transaction,\n+\t\t\t\t struct ref_update *update,\n+\t\t\t\t struct strbuf *err)\n+{\n+\tstruct ref_update *source = update->copy_from;\n+\tstruct copy_reflog_data data = { transaction, update->refname, err };\n+\tstruct object *object;\n+\tint ret;\n+\n+\toidcpy(&update->new_oid, &source->old_oid);\n+\tobject = parse_object(transaction->ref_store->repo, &update->new_oid);\n+\tif (!object || (is_branch(update->refname) && object->type != OBJ_COMMIT)) {\n+\t\tstrbuf_addf(err, _(\"cannot copy object %s to '%s'\"),\n+\t\t\t    oid_to_hex(&update->new_oid), update->refname);\n+\t\treturn -1;\n+\t}\n+\tif (object->type == OBJ_TAG &&\n+\t    !peel_object(transaction->ref_store->repo, &update->new_oid,\n+\t\t\t &update->peeled, PEEL_OBJECT_VERIFY_TAGGED_OBJECT_TYPE))\n+\t\tupdate->flags |= REF_HAVE_PEELED;\n+\n+\tif (!refs_reflog_exists(transaction->ref_store, source->refname))\n+\t\treturn 0;\n+\tret = refs_for_each_reflog_ent(transaction->ref_store, source->refname,\n+\t\t\t\t       copy_reflog_entry, &data);\n+\tif (ret && !err->len)\n+\t\tstrbuf_addf(err, _(\"cannot read reflog for '%s'\"), source->refname);\n+\treturn ret;\n+}\n+\n int refs_rename_ref(struct ref_store *refs, const char *oldref,\n \t\t    const char *newref, const char *logmsg)\n {\n-\tchar *msg;\n-\tint retval;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tstruct ref_transaction *transaction = ref_store_transaction_begin(refs, 0, &err);\n+\tint ret = -1;\n \n-\tmsg = normalize_reflog_message(logmsg);\n-\tretval = refs->be->rename_ref(refs, oldref, newref, msg);\n-\tfree(msg);\n-\treturn retval;\n+\tif (!transaction ||\n+\t    ref_transaction_rename(transaction, oldref, newref, logmsg, &err))\n+\t\tgoto out;\n+\tret = transaction->nr ? ref_transaction_commit(transaction, &err) : 0;\n+out:\n+\tif (ret)\n+\t\terror(\"%s\", err.buf);\n+\tref_transaction_free(transaction);\n+\tstrbuf_release(&err);\n+\treturn ret;\n }\n \n int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,\n-\t\t    const char *newref, const char *logmsg)\n+\t\t\t   const char *newref, const char *logmsg)\n {\n-\tchar *msg;\n-\tint retval;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tstruct ref_transaction *transaction = ref_store_transaction_begin(refs, 0, &err);\n+\tint ret = -1;\n \n-\tmsg = normalize_reflog_message(logmsg);\n-\tretval = refs->be->copy_ref(refs, oldref, newref, msg);\n-\tfree(msg);\n-\treturn retval;\n+\tif (!transaction ||\n+\t    ref_transaction_copy(transaction, oldref, newref, logmsg, &err))\n+\t\tgoto out;\n+\tret = transaction->nr ? ref_transaction_commit(transaction, &err) : 0;\n+out:\n+\tif (ret)\n+\t\terror(\"%s\", err.buf);\n+\tref_transaction_free(transaction);\n+\tstrbuf_release(&err);\n+\treturn ret;\n }\n \n const char *ref_update_original_update_refname(struct ref_update *update)\ndiff --git a/refs.h b/refs.h\nindex 7e80d0bdfb..2538c611cd 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -948,6 +948,22 @@ int ref_transaction_replace_reflog(struct ref_transaction *transaction,\n \t\t\t\t    const char *refname,\n \t\t\t\t    struct strbuf *err);\n \n+/*\n+ * Queue a copy or rename of a direct reference and its reflog. The source is\n+ * read again under lock during prepare; changes made by the preparing hook\n+ * are included. An existing destination is overwritten without dereferencing\n+ * it. Several independent copies, renames and ordinary updates may be queued\n+ * together. Each destination (and each source being renamed) must be unique.\n+ * Initial transactions and transactions allowing individual failures are not\n+ * supported. On failure, discard the transaction.\n+ */\n+int ref_transaction_copy(struct ref_transaction *transaction,\n+\t\t\t const char *oldref, const char *newref,\n+\t\t\t const char *logmsg, struct strbuf *err);\n+int ref_transaction_rename(struct ref_transaction *transaction,\n+\t\t\t   const char *oldref, const char *newref,\n+\t\t\t   const char *logmsg, struct strbuf *err);\n+\n /*\n  * Add a reference creation to transaction. new_oid is the value that\n  * the reference should have after the update; it must not be\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 70d1ff80c4..9e22f62d3f 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -74,6 +74,12 @@ extern int ignore_case;\n  */\n #define REF_LOG_VIA_SPLIT (1 << 14)\n \n+/* A D/F or case-only rename cannot lock a loose destination beside its source. */\n+#define REF_NEEDS_PACK (1 << 18)\n+\n+/* The source reflog was removed to make room for its rename destination. */\n+#define REF_RENAMED_LOG (1 << 19)\n+\n struct ref_lock {\n \tchar *ref_name;\n \tstruct lock_file lk;\n@@ -788,6 +794,37 @@ static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,\n \tlock->count = 1;\n \tfiles_ref_path(refs, &ref_file, refname);\n \n+\tif (update->copy_from && (update->copy_from->flags & REF_HAVE_NEW)) {\n+\t\tconst char *source = update->copy_from->refname;\n+\t\tsize_t source_len = strlen(source), dest_len = strlen(refname);\n+\t\tint source_is_parent = starts_with(refname, source) && refname[source_len] == '/';\n+\t\tint dest_is_parent = starts_with(source, refname) && source[dest_len] == '/';\n+\t\tint same_path = repo_ignore_case(refs->base.repo) && !strcasecmp(source, refname);\n+\n+\t\tif (source_is_parent || dest_is_parent || same_path) {\n+\t\t\tstruct string_list skip = STRING_LIST_INIT_NODUP;\n+\n+\t\t\tstring_list_insert(&skip, source);\n+\t\t\tret = refs_verify_refname_available(&refs->base, refname,\n+\t\t\t\t\t\t\t   extras, &skip, 0, err);\n+\t\t\tstring_list_clear(&skip, 0);\n+\t\t\tif (ret)\n+\t\t\t\tgoto error_return;\n+\t\t\t/* The existing source and its lock protect a child or case-only destination. */\n+\t\t\tif (dest_is_parent && repo_hold_lock_file_for_update_timeout(\n+\t\t\t\t\trefs->base.repo, &lock->lk, ref_file.buf, LOCK_NO_DEREF,\n+\t\t\t\t\tget_files_ref_lock_timeout_ms(refs->base.repo)) < 0) {\n+\t\t\t\tunable_to_lock_message(ref_file.buf, errno, err);\n+\t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n+\t\t\t\tgoto error_return;\n+\t\t\t}\n+\t\t\toidclr(&lock->old_oid, refs->base.repo->hash_algo);\n+\t\t\tupdate->flags |= REF_NEEDS_PACK;\n+\t\t\tret = 0;\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n retry:\n \tswitch (safe_create_leading_directories(refs->base.repo, ref_file.buf)) {\n \tcase SCLD_OK:\n@@ -2538,7 +2575,8 @@ static enum ref_transaction_error split_head_update(struct ref_update *update,\n \n \tnew_update = ref_transaction_add_update(\n \t\t\ttransaction, \"HEAD\",\n-\t\t\tupdate->flags | REF_LOG_ONLY | REF_NO_DEREF | REF_LOG_VIA_SPLIT,\n+\t\t\t(update->flags & ~REF_COPY_SOURCE) |\n+\t\t\tREF_LOG_ONLY | REF_NO_DEREF | REF_LOG_VIA_SPLIT,\n \t\t\t&update->new_oid, &update->old_oid, &update->peeled,\n \t\t\tNULL, NULL, update->committer_info, update->msg);\n \tnew_update->parent_update = update;\n@@ -2677,6 +2715,7 @@ static enum ref_transaction_error check_old_oid(struct ref_update *update,\n \n struct staged_reflog {\n \tstruct tempfile *file;\n+\tstruct tempfile *source_log;\n \tstruct ref_update **updates;\n \tsize_t nr, alloc;\n };\n@@ -2789,6 +2828,155 @@ static int install_reflog(const char *path, void *data)\n \treturn ret;\n }\n \n+static int move_reflog(const char *path, void *data)\n+{\n+\tstruct tempfile *tempfile = data;\n+\tint ret = rename(get_tempfile_path(tempfile), path);\n+\n+\tif (ret && errno == ENOTDIR)\n+\t\terrno = EISDIR;\n+\treturn ret;\n+}\n+\n+static int stage_source_reflog(struct files_ref_store *refs,\n+\t\t\t       struct ref_update *update,\n+\t\t\t       struct staged_reflog *log,\n+\t\t\t       struct strbuf *err)\n+{\n+\tstruct strbuf source = STRBUF_INIT;\n+\tstruct strbuf path = STRBUF_INIT;\n+\tint ret = -1;\n+\n+\tfiles_reflog_path(refs, &source, update->copy_from->refname);\n+\tif (!refs_reflog_exists(&refs->base, update->copy_from->refname)) {\n+\t\tret = 0;\n+\t\tgoto out;\n+\t}\n+\tfiles_reflog_path(refs, &path, \"refs/.tmp-reflog-source-XXXXXX\");\n+\tlog->source_log = mks_tempfile_m(path.buf, 0666);\n+\tif (!log->source_log || close_tempfile_gently(log->source_log) ||\n+\t    rename(source.buf, get_tempfile_path(log->source_log))) {\n+\t\tstrbuf_addf(err, \"cannot stage source reflog '%s': %s\",\n+\t\t\t    update->copy_from->refname, strerror(errno));\n+\t\tgoto out;\n+\t}\n+\ttry_remove_empty_parents(refs, update->copy_from->refname,\n+\t\t\t\t REMOVE_EMPTY_PARENTS_REFLOG);\n+\tupdate->copy_from->flags |= REF_RENAMED_LOG;\n+\tret = 0;\n+\n+out:\n+\tif (ret)\n+\t\tdelete_tempfile(&log->source_log);\n+\tstrbuf_release(&source);\n+\tstrbuf_release(&path);\n+\treturn ret;\n+}\n+\n+static int restore_source_reflog(struct files_ref_store *refs,\n+\t\t\t\t struct ref_update *update,\n+\t\t\t\t struct staged_reflog *log,\n+\t\t\t\t struct strbuf *err)\n+{\n+\tstruct strbuf source = STRBUF_INIT;\n+\tint ret;\n+\n+\tif (!log->source_log)\n+\t\treturn 0;\n+\tfiles_reflog_path(refs, &source, update->copy_from->refname);\n+\tret = raceproof_create_file(refs, source.buf, move_reflog,\n+\t\t\t\t    log->source_log);\n+\tif (ret) {\n+\t\tstruct strbuf recovery = STRBUF_INIT;\n+\n+\t\tstrbuf_addf(err, \"; cannot restore source reflog '%s': %s\",\n+\t\t\t    update->copy_from->refname, strerror(errno));\n+\t\tstrbuf_addf(&recovery, \"%s.recovery\",\n+\t\t\t    get_tempfile_path(log->source_log));\n+\t\tif (rename_tempfile(&log->source_log, recovery.buf))\n+\t\t\tstrbuf_addf(err, \"; cannot preserve its backup: %s\",\n+\t\t\t\t    strerror(errno));\n+\t\telse\n+\t\t\tstrbuf_addf(err, \"; backup saved as '%s'\", recovery.buf);\n+\t\tstrbuf_release(&recovery);\n+\t} else {\n+\t\tdelete_tempfile(&log->source_log);\n+\t}\n+\tstrbuf_release(&source);\n+\treturn ret;\n+}\n+\n+static int check_reflog_symlink(struct files_ref_store *refs,\n+\t\t\t\tconst char *refname, struct strbuf *err)\n+{\n+\tstruct strbuf path = STRBUF_INIT;\n+\tstruct stat st;\n+\tint ret = 0;\n+\n+\tfiles_reflog_path(refs, &path, refname);\n+\tif (!lstat(path.buf, &st)) {\n+\t\tif (S_ISLNK(st.st_mode)) {\n+\t\t\tstrbuf_addf(err, \"reflog for %s is a symlink\", refname);\n+\t\t\tret = -1;\n+\t\t}\n+\t} else if (errno != ENOENT && errno != ENOTDIR) {\n+\t\tstrbuf_addf(err, \"cannot stat reflog for %s: %s\", refname, strerror(errno));\n+\t\tret = -1;\n+\t}\n+\tstrbuf_release(&path);\n+\treturn ret;\n+}\n+\n+static struct ref_update *find_update_with_flag(struct ref_transaction *transaction,\n+\t\t\t\t\t\tconst char *refname,\n+\t\t\t\t\t\tunsigned int flag)\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 & flag) && !strcmp(update->refname, refname))\n+\t\t\treturn update;\n+\t}\n+\treturn NULL;\n+}\n+\n+static int prepare_copy_reflog(struct files_ref_store *refs,\n+\t\t\t       struct ref_transaction *transaction,\n+\t\t\t       struct ref_update *update, struct strbuf *err)\n+{\n+\tenum log_refs_config config = files_ref_store_write_options(refs)->log_all_ref_updates;\n+\tint source_has_log;\n+\tint write_log;\n+\n+\tif (check_reflog_symlink(refs, update->copy_from->refname, err) ||\n+\t    check_reflog_symlink(refs, update->refname, err))\n+\t\treturn -1;\n+\tsource_has_log = refs_reflog_exists(&refs->base, update->copy_from->refname);\n+\t/* A copy without source history only appends to an existing destination log. */\n+\tif (!source_has_log && !(update->copy_from->flags & REF_HAVE_NEW)) {\n+\t\tstruct ref_update *replacement = find_update_with_flag(\n+\t\t\ttransaction, update->refname, REF_REPLACE_REFLOG);\n+\n+\t\tif (!replacement)\n+\t\t\tBUG(\"copy destination without a reflog replacement\");\n+\t\treplacement->flags &= ~REF_REPLACE_REFLOG;\n+\t}\n+\tif (config == LOG_REFS_UNSET)\n+\t\tconfig = is_bare_repository(refs->base.repo) ? LOG_REFS_NONE : LOG_REFS_NORMAL;\n+\twrite_log = source_has_log || should_autocreate_reflog(config, update->refname) ||\n+\t\t(!(update->copy_from->flags & REF_HAVE_NEW) &&\n+\t\t refs_reflog_exists(&refs->base, update->refname));\n+\tif (ref_transaction_prepare_copy(transaction, update, err))\n+\t\treturn -1;\n+\tif (!write_log)\n+\t\treturn 0;\n+\treturn ref_transaction_update_reflog(transaction, update->refname,\n+\t\t\t&update->new_oid, &update->new_oid, NULL, update->msg,\n+\t\t\ttransaction->max_index + 1, err);\n+}\n+\n /*\n  * Prepare for carrying out update:\n  * - Lock the reference referred to by update.\n@@ -2819,6 +3007,8 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re\n \tfiles_assert_main_repository(refs, \"lock_ref_for_update\");\n \n \tbackend_data = transaction->backend_data;\n+\tif (update->copy_from && prepare_copy_reflog(refs, transaction, update, err))\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \n \tif ((update->flags & REF_HAVE_NEW) && ref_update_has_null_new_value(update))\n \t\tupdate->flags |= REF_DELETING;\n@@ -2828,6 +3018,8 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re\n \t\tif (ret)\n \t\t\tgoto out;\n \t}\n+\tif (update->copy_from)\n+\t\tupdate->flags |= REF_SKIP_CREATE_REFLOG;\n \n \tlock = strmap_get(&backend_data->ref_locks, update->refname);\n \tif (lock) {\n@@ -2849,6 +3041,17 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re\n \t}\n \n \tupdate->backend_data = lock;\n+\tif (update->copy_from) {\n+\t\toidcpy(&update->old_oid, &lock->old_oid);\n+\t\tif (update->type & REF_ISSYMREF)\n+\t\t\tupdate->old_target = xstrdup(referent.buf);\n+\t\tupdate->flags |= REF_HAVE_OLD;\n+\t}\n+\tif ((update->flags & REF_COPY_SOURCE) &&\n+\t    ref_update_record_copy_source(update, &lock->old_oid, err)) {\n+\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n+\t\tgoto out;\n+\t}\n \n \tif (update->flags & REF_LOG_VIA_SPLIT) {\n \t\tstruct ref_lock *parent_lock;\n@@ -2974,7 +3177,7 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re\n \t\tupdate->flags |= REF_NEEDS_COMMIT;\n \t} else if ((update->flags & REF_HAVE_NEW) &&\n \t\t   !(update->flags & REF_DELETING) &&\n-\t\t   !(update->flags & REF_LOG_ONLY)) {\n+\t\t   !(update->flags & (REF_LOG_ONLY | REF_NEEDS_PACK))) {\n \t\tif (!(update->type & REF_ISSYMREF) &&\n \t\t    oideq(&lock->old_oid, &update->new_oid)) {\n \t\t\t/*\n@@ -3003,7 +3206,7 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re\n \t\t\t}\n \t\t}\n \t}\n-\tif (!(update->flags & REF_NEEDS_COMMIT)) {\n+\tif (!(update->flags & (REF_NEEDS_COMMIT | REF_NEEDS_PACK))) {\n \t\t/*\n \t\t * We didn't call write_ref_to_lockfile(), so\n \t\t * the lockfile is still open. Close it to\n@@ -3054,6 +3257,7 @@ static void files_transaction_cleanup(struct files_ref_store *refs,\n \t\t\tstruct staged_reflog *log = entry->value;\n \n \t\t\tdelete_tempfile(&log->file);\n+\t\t\tdelete_tempfile(&log->source_log);\n \t\t\tfree(log->updates);\n \t\t\tfree(log);\n \t\t}\n@@ -3162,7 +3366,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\t\tgoto cleanup;\n \t\t}\n \n-\t\tif (update->flags & REF_DELETING &&\n+\t\tif (update->flags & (REF_DELETING | REF_NEEDS_PACK) &&\n \t\t    !(update->flags & REF_LOG_ONLY) &&\n \t\t    !(update->flags & REF_IS_PRUNING)) {\n \t\t\t/*\n@@ -3187,6 +3391,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\t\t\t\tREF_HAVE_NEW | REF_NO_DEREF,\n \t\t\t\t\t&update->new_oid, NULL, NULL,\n \t\t\t\t\tNULL, NULL, NULL, NULL);\n+\t\t\tif (update->copy_from || (update->flags & REF_COPY_SOURCE))\n+\t\t\t\tpacked_transaction->flags |= REF_TRANSACTION_FLAG_INTERNAL;\n \t\t}\n \t}\n \n@@ -3478,11 +3684,18 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \n \tfor (i = 0; i < transaction->nr; i++) {\n \t\tstruct ref_update *update = transaction->updates[i];\n+\t\tstruct ref_update *operation;\n \t\tstruct staged_reflog *log;\n \n \t\tif (!(update->flags & REF_REPLACE_REFLOG))\n \t\t\tcontinue;\n \t\tlog = strmap_get(&backend_data->reflog_files, update->refname);\n+\t\toperation = find_update_with_flag(transaction, update->refname,\n+\t\t\t\t\t\t  REF_NEEDS_PACK);\n+\t\tif (operation && stage_source_reflog(refs, operation, log, err)) {\n+\t\t\tret = -1;\n+\t\t\tgoto cleanup;\n+\t\t}\n \t\tstrbuf_reset(&sb);\n \t\tfiles_reflog_path(refs, &sb, update->refname);\n \t\tif (!log->file) {\n@@ -3496,6 +3709,8 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \t\tif (ret) {\n \t\t\tstrbuf_addf(err, \"cannot replace reflog '%s': %s\",\n \t\t\t\t    update->refname, strerror(errno));\n+\t\t\tif (operation)\n+\t\t\t\trestore_source_reflog(refs, operation, log, err);\n \t\t}\n \t\tif (ret)\n \t\t\tgoto cleanup;\n@@ -3560,7 +3775,7 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \n \t\tif (update->flags & REF_DELETING &&\n \t\t    !(update->flags & REF_LOG_ONLY) &&\n-\t\t    !(update->flags & REF_IS_PRUNING)) {\n+\t\t    !(update->flags & (REF_IS_PRUNING | REF_RENAMED_LOG))) {\n \t\t\tstrbuf_reset(&sb);\n \t\t\tfiles_reflog_path(refs, &sb, update->refname);\n \t\t\tif (!unlink_or_warn(sb.buf))\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 19fdf39d1d..2746ce5b49 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -52,6 +52,9 @@ struct ref_transaction;\n /* Replace a reflog with the explicit log-only updates in the transaction. */\n #define REF_REPLACE_REFLOG (1 << 16)\n \n+/* A source whose value and reflog are consumed by a later update. */\n+#define REF_COPY_SOURCE (1 << 17)\n+\n /*\n  * Return the length of time to retry acquiring a loose reference lock\n  * before giving up, in milliseconds:\n@@ -161,6 +164,9 @@ struct ref_update {\n \t */\n \tstruct ref_update *parent_update;\n \n+\t/* The source is queued first, so backends lock it before the destination. */\n+\tstruct ref_update *copy_from;\n+\n \tconst char refname[FLEX_ARRAY];\n };\n \n@@ -193,6 +199,13 @@ struct ref_update *ref_transaction_add_update(\n \t\tconst char *committer_info,\n \t\tconst char *msg);\n \n+int ref_update_record_copy_source(struct ref_update *update,\n+\t\t\t\t  const struct object_id *oid,\n+\t\t\t\t  struct strbuf *err);\n+int ref_transaction_prepare_copy(struct ref_transaction *transaction,\n+\t\t\t\t struct ref_update *update,\n+\t\t\t\t struct strbuf *err);\n+\n /*\n  * Transaction states.\n  *\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 203b111f3d..72c1596c11 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1076,6 +1076,22 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor\n \tstruct object_id current_oid = {0};\n \tconst char *rewritten_ref;\n \n+\tif (u->copy_from) {\n+\t\tconst struct object_id *zero = null_oid(refs->base.repo->hash_algo);\n+\n+\t\tif (ref_transaction_prepare_copy(transaction, u, err))\n+\t\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n+\t\tif ((u->copy_from->flags & REF_HAVE_NEW) &&\n+\t\t    ref_transaction_update_reflog(transaction, u->refname,\n+\t\t\t\tzero, &u->new_oid, NULL, u->msg,\n+\t\t\t\ttransaction->max_index + 1, err))\n+\t\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n+\t\tif (ref_transaction_update_reflog(transaction, u->refname,\n+\t\t\t\t&u->new_oid, zero, NULL, u->msg,\n+\t\t\t\ttransaction->max_index + 1, err))\n+\t\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n+\t}\n+\n \t/*\n \t * There is no need to reload the respective backends here as\n \t * we have already reloaded them when preparing the transaction\n@@ -1126,15 +1142,27 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor\n \n \t\tref_transaction_add_update(\n \t\t\ttransaction, \"HEAD\",\n-\t\t\tu->flags | REF_LOG_ONLY | REF_NO_DEREF,\n+\t\t\t(u->flags & ~REF_COPY_SOURCE) |\n+\t\t\tREF_LOG_ONLY | REF_NO_DEREF,\n \t\t\t&u->new_oid, &u->old_oid, &u->peeled, NULL, NULL,\n \t\t\tNULL, u->msg);\n \t}\n+\tif (u->copy_from)\n+\t\tu->flags |= REF_SKIP_CREATE_REFLOG;\n \n \tret = reftable_backend_read_ref(be, rewritten_ref,\n \t\t\t\t\t&current_oid, referent, &u->type);\n \tif (ret < 0)\n \t\treturn REF_TRANSACTION_ERROR_GENERIC;\n+\tif (u->copy_from) {\n+\t\toidcpy(&u->old_oid, &current_oid);\n+\t\tif (u->type & REF_ISSYMREF)\n+\t\t\tu->old_target = xstrdup(referent->buf);\n+\t\tu->flags |= REF_HAVE_OLD;\n+\t}\n+\tif ((u->flags & REF_COPY_SOURCE) &&\n+\t    ref_update_record_copy_source(u, &current_oid, err))\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \tif (ret > 0 && !ref_update_expects_existing_old_ref(u)) {\n \t\tstruct string_list_item *item;\n \t\t/*\n@@ -1319,6 +1347,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\treftable_be_downcast(ref_store, REF_STORE_WRITE|REF_STORE_MAIN, \"ref_transaction_prepare\");\n \tstruct strbuf referent = STRBUF_INIT, head_referent = STRBUF_INIT;\n \tstruct string_list refnames_to_check = STRING_LIST_INIT_NODUP;\n+\tstruct string_list renamed_sources = STRING_LIST_INIT_NODUP;\n \tstruct reftable_transaction_data *tx_data = NULL;\n \tstruct reftable_backend *be;\n \tstruct object_id head_oid;\n@@ -1338,6 +1367,9 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t * that will be modified during the transaction.\n \t */\n \tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\t\tif (update->copy_from && (update->copy_from->flags & REF_HAVE_NEW))\n+\t\t\tstring_list_insert(&renamed_sources, update->copy_from->refname);\n \t\tret = prepare_transaction_update(NULL, refs, tx_data,\n \t\t\t\t\t\t transaction->updates[i], err);\n \t\tif (ret)\n@@ -1390,7 +1422,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t}\n \n \tret = refs_verify_refnames_available(ref_store, &refnames_to_check,\n-\t\t\t\t\t     &transaction->refnames, NULL,\n+\t\t\t\t\t     &transaction->refnames, &renamed_sources,\n \t\t\t\t\t     transaction,\n \t\t\t\t\t     transaction->flags & REF_TRANSACTION_FLAG_INITIAL,\n \t\t\t\t\t     err);\n@@ -1411,6 +1443,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \tstrbuf_release(&referent);\n \tstrbuf_release(&head_referent);\n \tstring_list_clear(&refnames_to_check, 1);\n+\tstring_list_clear(&renamed_sources, 0);\n \n \treturn ret;\n }\ndiff --git a/t/helper/test-ref-store.c b/t/helper/test-ref-store.c\nindex 7ce10c28af..ec7728334c 100644\n--- a/t/helper/test-ref-store.c\n+++ b/t/helper/test-ref-store.c\n@@ -185,6 +185,41 @@ static int cmd_reflog_transaction(struct ref_store *refs, const char **argv)\n \treturn ret;\n }\n \n+static int cmd_copy_transaction(struct ref_store *refs, const char **argv)\n+{\n+\tstruct strbuf err = STRBUF_INIT;\n+\tstruct ref_transaction *transaction = ref_store_transaction_begin(refs, 0, &err);\n+\tint ret = !transaction;\n+\n+\twhile (!ret && *argv) {\n+\t\tconst char *operation = *argv++;\n+\t\tconst char *source = notnull(*argv++, \"source\");\n+\t\tconst char *destination = notnull(*argv++, \"destination\");\n+\n+\t\tif (!strcmp(operation, \"rename\"))\n+\t\t\tret = ref_transaction_rename(transaction, source, destination, \"rename\", &err);\n+\t\telse if (!strcmp(operation, \"copy\"))\n+\t\t\tret = ref_transaction_copy(transaction, source, destination, \"copy\", &err);\n+\t\telse if (!strcmp(operation, \"update\")) {\n+\t\t\tstruct object_id oid;\n+\n+\t\t\tif (get_oid_hex(destination, &oid))\n+\t\t\t\tdie(\"invalid object ID: %s\", destination);\n+\t\t\tret = ref_transaction_update(transaction, source, &oid,\n+\t\t\t\t\t\t     NULL, NULL, NULL, 0, \"update\", &err);\n+\t\t} else {\n+\t\t\tdie(\"unknown operation: %s\", operation);\n+\t\t}\n+\t}\n+\tif (!ret)\n+\t\tret = ref_transaction_commit(transaction, &err);\n+\tif (ret)\n+\t\terror(\"%s\", err.buf);\n+\tref_transaction_free(transaction);\n+\tstrbuf_release(&err);\n+\treturn ret;\n+}\n+\n static int each_ref(const struct reference *ref, void *cb_data UNUSED)\n {\n \tprintf(\"%s %s 0x%x\\n\", oid_to_hex(ref->oid), ref->name, ref->flags);\n@@ -348,6 +383,7 @@ static struct command commands[] = {\n \t{ \"delete-refs\", cmd_delete_refs },\n \t{ \"rename-ref\", cmd_rename_ref },\n \t{ \"reflog-transaction\", cmd_reflog_transaction },\n+\t{ \"copy-transaction\", cmd_copy_transaction },\n \t{ \"for-each-ref\", cmd_for_each_ref },\n \t{ \"for-each-ref--exclude\", cmd_for_each_ref__exclude },\n \t{ \"resolve-ref\", cmd_resolve_ref },\ndiff --git a/t/perf/p1424-ref-copy-rename.sh b/t/perf/p1424-ref-copy-rename.sh\nnew file mode 100755\nindex 0000000000..a901129b00\n--- /dev/null\n+++ b/t/perf/p1424-ref-copy-rename.sh\n@@ -0,0 +1,48 @@\n+#!/bin/sh\n+\n+test_description='Copy and rename references with short and long reflogs'\n+\n+. ./perf-lib.sh\n+\n+test_perf_fresh_repo\n+\n+for entries in 10 10000\n+do\n+\texport entries\n+\ttest_expect_success \"setup $entries reflog entries\" '\n+\t\tgit init --ref-format=files \"repo-$entries\" &&\n+\t\t(\n+\t\t\tcd \"repo-$entries\" &&\n+\t\t\ttest_commit A &&\n+\t\t\tgit branch source &&\n+\t\t\toid=$(git rev-parse HEAD) &&\n+\t\t\tawk -v oid=\"$oid\" -v count=\"$entries\" '\\''\n+\t\t\t\tBEGIN {\n+\t\t\t\t\tfor (i = 0; i < count; i++)\n+\t\t\t\t\t\tprintf \"%s %s A <a@example.com> %d +0000\\tentry %d\\n\", oid, oid, 1700000000 + i, i\n+\t\t\t\t}\n+\t\t\t'\\'' >.git/logs/refs/heads/source &&\n+\t\t\tif test \"$GIT_TEST_DEFAULT_REF_FORMAT\" = reftable\n+\t\t\tthen\n+\t\t\t\tgit refs migrate --ref-format=reftable\n+\t\t\tfi\n+\t\t)\n+\t'\n+\n+\ttest_perf \"copy $entries reflog entries (10 operations)\" '\n+\t\tfor i in $(test_seq 10)\n+\t\tdo\n+\t\t\tgit -C \"repo-$entries\" branch -C source destination || return 1\n+\t\tdone\n+\t'\n+\n+\ttest_perf \"rename $entries reflog entries (10 operations)\" '\n+\t\tfor i in $(test_seq 5)\n+\t\tdo\n+\t\t\tgit -C \"repo-$entries\" branch -m source renamed &&\n+\t\t\tgit -C \"repo-$entries\" branch -m renamed source || return 1\n+\t\tdone\n+\t'\n+done\n+\n+test_done\ndiff --git a/t/t1424-ref-copy-transaction.sh b/t/t1424-ref-copy-transaction.sh\nnew file mode 100755\nindex 0000000000..4c7592795f\n--- /dev/null\n+++ b/t/t1424-ref-copy-transaction.sh\n@@ -0,0 +1,352 @@\n+#!/bin/sh\n+\n+test_description='reference transactions for copy and rename'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+snapshot () {\n+\tgit for-each-ref --format=\"%(refname) %(objectname) %(symref)\" >\"$1.refs\" &&\n+\tfor ref in HEAD refs/heads/source refs/heads/destination\n+\tdo\n+\t\tif git reflog exists \"$ref\"\n+\t\tthen\n+\t\t\ttest-tool ref-store main for-each-reflog-ent \"$ref\"\n+\t\telse\n+\t\t\techo missing\n+\t\tfi >\"$1.$(basename \"$ref\").log\" || return 1\n+\tdone\n+}\n+\n+compare_snapshot () {\n+\tfor suffix in refs HEAD.log source.log destination.log\n+\tdo\n+\t\ttest_cmp \"$1.$suffix\" \"$2.$suffix\" || return 1\n+\tdone\n+}\n+\n+test_expect_success 'setup' '\n+\ttest_commit A &&\n+\ttest_commit B &&\n+\tA=$(git rev-parse A) &&\n+\tB=$(git rev-parse B)\n+'\n+\n+for operation in rename copy\n+do\n+\ttest_expect_success \"$operation reports one logical transaction\" '\n+\t\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\t\tgit branch -f source \"$A\" &&\n+\t\tgit branch -f destination \"$B\" &&\n+\t\tmkdir -p hooks &&\n+\t\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\t\techo \"$1\" >>actual &&\n+\t\t\tcat >>actual\n+\t\tEOF\n+\t\tgit config core.hooksPath hooks &&\n+\t\t>actual &&\n+\t\ttest-tool ref-store main copy-transaction \"$operation\" refs/heads/source refs/heads/destination &&\n+\t\t{\n+\t\t\techo preparing &&\n+\t\t\tif test \"$operation\" = rename\n+\t\t\tthen\n+\t\t\t\techo \"$ZERO_OID $ZERO_OID refs/heads/source\"\n+\t\t\tfi &&\n+\t\t\techo \"$ZERO_OID $A refs/heads/destination\" &&\n+\t\t\tfor state in prepared committed\n+\t\t\tdo\n+\t\t\t\techo \"$state\" &&\n+\t\t\t\tif test \"$operation\" = rename\n+\t\t\t\tthen\n+\t\t\t\t\techo \"$A $ZERO_OID refs/heads/source\"\n+\t\t\t\tfi &&\n+\t\t\t\techo \"$B $A refs/heads/destination\" || return 1\n+\t\t\tdone\n+\t\t} >expect &&\n+\t\ttest_cmp expect actual\n+\t'\n+\n+\tfor state in preparing prepared\n+\tdo\n+\t\ttest_expect_success \"$operation rejected at $state leaves refs and full reflogs unchanged\" '\n+\t\t\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\t\t\tgit branch -f source \"$A\" &&\n+\t\t\tgit branch -f destination \"$B\" &&\n+\t\t\tgit symbolic-ref HEAD refs/heads/source &&\n+\t\t\ttest_when_finished \"git -c core.hooksPath=/dev/null symbolic-ref HEAD refs/heads/main\" &&\n+\t\t\tsnapshot before &&\n+\t\t\twrite_script hooks/reference-transaction <<-EOF &&\n+\t\t\t\ttest \"\\$1\" != \"$state\"\n+\t\t\tEOF\n+\t\t\tgit config core.hooksPath hooks &&\n+\t\t\ttest_must_fail test-tool ref-store main copy-transaction \"$operation\" refs/heads/source refs/heads/destination &&\n+\t\t\tsnapshot after &&\n+\t\t\tcompare_snapshot before after\n+\t\t'\n+\tdone\n+done\n+\n+test_expect_success 'prepared hook sees old refs and cannot modify the source' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch -f source \"$A\" &&\n+\tgit branch -f destination \"$B\" &&\n+\twrite_script hooks/reference-transaction <<-EOF &&\n+\t\ttest \"\\$1\" = prepared || exit 0\n+\t\tgit rev-parse refs/heads/source >source-seen &&\n+\t\tgit rev-parse refs/heads/destination >destination-seen &&\n+\t\tif git -c core.hooksPath=/dev/null -c core.filesRefLockTimeout=0 \\\n+\t\t\tupdate-ref refs/heads/source $B\n+\t\tthen\n+\t\t\texit 1\n+\t\tfi\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\tgit branch -M source destination &&\n+\techo \"$A\" >expect &&\n+\ttest_cmp expect source-seen &&\n+\techo \"$B\" >expect &&\n+\ttest_cmp expect destination-seen &&\n+\ttest_cmp_rev \"$A\" destination\n+'\n+\n+test_expect_success 'preparing may update source and its reflog' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch source \"$A\" &&\n+\twrite_script hooks/reference-transaction <<-EOF &&\n+\t\ttest \"\\$1\" = preparing || exit 0\n+\t\tgit -c core.hooksPath=/dev/null update-ref -m concurrent refs/heads/source $B\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\tgit branch -M source destination &&\n+\ttest_cmp_rev \"$B\" destination &&\n+\tgit reflog show --format=%gs destination >actual &&\n+\ttest_grep concurrent actual\n+'\n+\n+test_expect_success 'rename without a source reflog can be rejected without losing destination history' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit -c core.logAllRefUpdates=false branch source \"$A\" &&\n+\ttest_must_fail git reflog exists refs/heads/source &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/destination >before &&\n+\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\ttest_must_fail git branch -M source destination &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/destination >after &&\n+\ttest_cmp before after\n+'\n+\n+for names in \"parent parent/child\" \"child/branch child\"\n+do\n+\tset -- $names\n+\tfrom=$1 to=$2\n+\ttest_expect_success \"D/F rename $from to $to can be aborted and committed\" '\n+\t\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\t\tgit branch \"$from\" \"$A\" &&\n+\t\ttest-tool ref-store main for-each-reflog-ent \"refs/heads/$from\" >before &&\n+\t\twrite_script hooks/reference-transaction <<-EOF &&\n+\t\t\ttest \"\\$1\" = prepared || exit 0\n+\t\t\tgit rev-parse refs/heads/$from >seen\n+\t\t\texit 1\n+\t\tEOF\n+\t\tgit config core.hooksPath hooks &&\n+\t\ttest_must_fail git branch -m \"$from\" \"$to\" &&\n+\t\techo \"$A\" >expect &&\n+\t\ttest_cmp expect seen &&\n+\t\ttest_cmp_rev \"$A\" \"$from\" &&\n+\t\ttest-tool ref-store main for-each-reflog-ent \"refs/heads/$from\" >after &&\n+\t\ttest_cmp before after &&\n+\t\tgit -c core.hooksPath=/dev/null branch -m \"$from\" \"$to\" &&\n+\t\ttest_cmp_rev \"$A\" \"$to\"\n+\t'\n+done\n+\n+test_expect_success REFFILES,POSIXPERM 'D/F reflog installation failure restores source history' '\n+\ttest_when_finished \"chmod u+w .git/logs/refs/heads\" &&\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch rollback/branch \"$A\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/rollback/branch >before &&\n+\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" = prepared || exit 0\n+\t\tchmod a-w .git/logs/refs/heads\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\ttest_must_fail git branch -m rollback/branch rollback &&\n+\tchmod u+w .git/logs/refs/heads &&\n+\ttest_cmp_rev \"$A\" rollback/branch &&\n+\ttest_must_fail git show-ref --verify refs/heads/rollback &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/rollback/branch >after &&\n+\ttest_cmp before after\n+'\n+\n+test_expect_success 'multiple renames, a copy and an ordinary update compose' '\n+\tgit branch first \"$A\" &&\n+\tgit branch second \"$B\" &&\n+\tgit branch third \"$A\" &&\n+\ttest-tool ref-store main copy-transaction \\\n+\t\trename refs/heads/first refs/heads/first-new \\\n+\t\trename refs/heads/second refs/heads/second-new \\\n+\t\tcopy refs/heads/third refs/heads/third-new \\\n+\t\tupdate refs/heads/fourth \"$B\" &&\n+\ttest_must_fail git show-ref --verify refs/heads/first &&\n+\ttest_must_fail git show-ref --verify refs/heads/second &&\n+\ttest_cmp_rev \"$A\" first-new &&\n+\ttest_cmp_rev \"$B\" second-new &&\n+\ttest_cmp_rev \"$A\" third-new &&\n+\ttest_cmp_rev \"$A\" third &&\n+\ttest_cmp_rev \"$B\" fourth\n+'\n+\n+test_expect_success 'copy preserves complete source history and replaces destination history' '\n+\tgit branch history-source \"$A\" &&\n+\tgit update-ref -m history-step refs/heads/history-source \"$B\" &&\n+\tgit branch history-destination \"$B\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/history-source >before &&\n+\tgit branch -C history-source history-destination &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/history-source >after &&\n+\ttest_cmp before after &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/history-destination >copied &&\n+\tsed \"$ d\" copied >history &&\n+\ttest_cmp before history &&\n+\ttest_grep \"Branch: copied refs/heads/history-source to refs/heads/history-destination\" copied\n+'\n+\n+test_expect_success 'packed rename reports no internal transactions' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch packed-source \"$A\" &&\n+\tgit pack-refs --all &&\n+\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\techo \"$1\" >>states\n+\t\tcat >/dev/null\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\tgit branch -m packed-source packed-destination &&\n+\tprintf \"%s\\n\" preparing prepared committed >expect &&\n+\ttest_cmp expect states\n+'\n+\n+test_expect_success REFFILES 'unrelated forced copies use independent reflog staging files' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch nested-a \"$A\" &&\n+\tgit branch nested-b \"$B\" &&\n+\tgit branch outer-a \"$A\" &&\n+\tgit branch outer-b \"$B\" &&\n+\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" = prepared || exit 0\n+\t\tgit -c core.hooksPath=/dev/null branch -C nested-a nested-b\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\tgit branch -C outer-a outer-b &&\n+\ttest_cmp_rev \"$A\" outer-b &&\n+\ttest_cmp_rev \"$A\" nested-b\n+'\n+\n+test_expect_success REFFILES 'rename with reflogs disabled does not create a reflog' '\n+\tgit -c core.logAllRefUpdates=false branch no-log \"$A\" &&\n+\tgit -c core.logAllRefUpdates=false branch -m no-log still-no-log &&\n+\ttest_must_fail git reflog exists refs/heads/still-no-log\n+'\n+\n+test_expect_success REFFILES 'copy without source history retains existing destination history' '\n+\tgit -c core.logAllRefUpdates=false branch no-copy-log \"$A\" &&\n+\tgit branch existing-copy-log \"$B\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/existing-copy-log >before &&\n+\tgit branch -C no-copy-log existing-copy-log &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/existing-copy-log >after &&\n+\tsed \"$ d\" after >history &&\n+\ttest_cmp before history\n+'\n+\n+test_expect_success REFFILES,CASE_INSENSITIVE_FS 'case-only rename retains history and supports abort' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch case-source \"$A\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/case-source >before &&\n+\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\ttest_must_fail git branch -M case-source CASE-SOURCE &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/case-source >after &&\n+\ttest_cmp before after &&\n+\tgit -c core.hooksPath=/dev/null branch -M case-source CASE-SOURCE &&\n+\tgit for-each-ref --format=\"%(refname)\" refs/heads/CASE-SOURCE >actual &&\n+\techo refs/heads/CASE-SOURCE >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success REFFILES,SYMLINKS 'symlink source reflog is rejected without changing refs' '\n+\tgit branch symlink-source \"$A\" &&\n+\tmv .git/logs/refs/heads/symlink-source saved-log &&\n+\tln -s \"$PWD/saved-log\" .git/logs/refs/heads/symlink-source &&\n+\ttest_must_fail git branch -m symlink-source symlink-destination &&\n+\ttest_cmp_rev \"$A\" symlink-source &&\n+\ttest_must_fail git show-ref --verify refs/heads/symlink-destination\n+'\n+\n+for operation in copy rename\n+do\n+\ttest_expect_success \"$operation over HEAD referent preserves HEAD history\" '\n+\t\tgit branch -f head-source \"$A\" &&\n+\t\tgit update-ref refs/heads/main \"$B\" &&\n+\t\ttest-tool ref-store main for-each-reflog-ent HEAD >before &&\n+\t\ttest-tool ref-store main copy-transaction \"$operation\" refs/heads/head-source refs/heads/main &&\n+\t\ttest-tool ref-store main for-each-reflog-ent HEAD >after &&\n+\t\tsed \"$ d\" after >history &&\n+\t\ttest_cmp before history &&\n+\t\ttest_cmp_rev \"$A\" HEAD\n+\t'\n+done\n+\n+test_expect_success 'preparing may delete source but cannot cause a partial rename' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch race-source \"$A\" &&\n+\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" = preparing || exit 0\n+\t\tgit -c core.hooksPath=/dev/null update-ref -d refs/heads/race-source\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\ttest_must_fail git branch -m race-source race-destination &&\n+\ttest_must_fail git show-ref --verify refs/heads/race-source &&\n+\ttest_must_fail git show-ref --verify refs/heads/race-destination\n+'\n+\n+test_expect_success 'copy cannot replace a D/F-conflicting source' '\n+\tgit branch conflict-source \"$A\" &&\n+\ttest_must_fail git branch -c conflict-source conflict-source/child &&\n+\ttest_cmp_rev \"$A\" conflict-source &&\n+\ttest_must_fail git show-ref --verify refs/heads/conflict-source/child\n+'\n+\n+test_expect_success REFFILES,POSIXPERM 'unreadable source reflog leaves refs and history unchanged' '\n+\tgit branch unreadable \"$A\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/unreadable >before &&\n+\ttest_when_finished \"chmod u+r .git/logs/refs/heads/unreadable\" &&\n+\tchmod a-r .git/logs/refs/heads/unreadable &&\n+\ttest_must_fail git branch -m unreadable readable &&\n+\tchmod u+r .git/logs/refs/heads/unreadable &&\n+\ttest_cmp_rev \"$A\" unreadable &&\n+\ttest_must_fail git show-ref --verify refs/heads/readable &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/unreadable >after &&\n+\ttest_cmp before after\n+'\n+\n+test_expect_success REFFILES 'destination lock failure preserves source and destination history' '\n+\tgit branch -f source \"$A\" &&\n+\tgit branch -f destination \"$B\" &&\n+\tsnapshot before &&\n+\ttest_when_finished \"rm -f .git/refs/heads/destination.lock\" &&\n+\t>.git/refs/heads/destination.lock &&\n+\ttest_must_fail git -c core.filesRefLockTimeout=0 branch -M source destination &&\n+\tsnapshot after &&\n+\tcompare_snapshot before after\n+'\n+\n+test_expect_success REFFILES 'staged reflog files are cleaned up after success and abort' '\n+\tfind .git/logs -name \".tmp-reflog-*\" >actual &&\n+\ttest_must_be_empty actual\n+'\n+\n+test_done\n-- \n2.39.3 (Apple Git-146)\n\n\n"},{"id":"554405","messageId":"83fa644fb3dbf1807e232cac22f9dd7528b766c1.1791395643.git.maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"cover.1791395643.git.maciej.ciemborowicz@gmail.com","subject":"[PATCH v3 4/4] refs: remove backend-specific copy and rename callbacks","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-07T18:05:17Z","receivedAt":"2026-10-07T18:05:17Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"Copy and rename now use the transaction API for refs and reflogs. Remove\nthe unused callbacks, backend implementations, and their private\nhelpers. The files backend no longer needs a shared temporary reflog\nname or rollback writes that reconstruct previously visible refs and\nlogs.\n\nSigned-off-by: Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com>\n---\n refs/debug.c            |  24 ---\n refs/files-backend.c    | 334 ----------------------------------------\n refs/packed-backend.c   |   2 -\n refs/refs-internal.h    |   9 --\n refs/reftable-backend.c | 287 ----------------------------------\n 5 files changed, 656 deletions(-)\n\ndiff --git a/refs/debug.c b/refs/debug.c\nindex 639db0f26e..b4991f71ae 100644\n--- a/refs/debug.c\n+++ b/refs/debug.c\n@@ -143,28 +143,6 @@ static int debug_optimize_required(struct ref_store *ref_store,\n \treturn res;\n }\n \n-static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,\n-\t\t\t    const char *newref, const char *logmsg)\n-{\n-\tstruct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;\n-\tint res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref,\n-\t\t\t\t\t      logmsg);\n-\ttrace_printf_key(&trace_refs, \"rename_ref: %s -> %s \\\"%s\\\": %d\\n\", oldref, newref,\n-\t\tlogmsg, res);\n-\treturn res;\n-}\n-\n-static int debug_copy_ref(struct ref_store *ref_store, const char *oldref,\n-\t\t\t  const char *newref, const char *logmsg)\n-{\n-\tstruct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;\n-\tint res =\n-\t\tdrefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg);\n-\ttrace_printf_key(&trace_refs, \"copy_ref: %s -> %s \\\"%s\\\": %d\\n\", oldref, newref,\n-\t\tlogmsg, res);\n-\treturn res;\n-}\n-\n struct debug_ref_iterator {\n \tstruct ref_iterator base;\n \tstruct ref_iterator *iter;\n@@ -453,8 +431,6 @@ struct ref_storage_be refs_be_debug = {\n \t.optimize = debug_optimize,\n \t.optimize_required = debug_optimize_required,\n \n-\t.rename_ref = debug_rename_ref,\n-\t.copy_ref = debug_copy_ref,\n \n \t.iterator_begin = debug_ref_iterator_begin,\n \t.read_raw_ref = debug_read_raw_ref,\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 9e22f62d3f..a00cfc6be5 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1623,273 +1623,6 @@ static int files_optimize_required(struct ref_store *ref_store,\n \treturn 0;\n }\n \n-/*\n- * People using contrib's git-new-workdir have .git/logs/refs ->\n- * /some/other/path/.git/logs/refs, and that may live on another device.\n- *\n- * IOW, to avoid cross device rename errors, the temporary renamed log must\n- * live into logs/refs.\n- */\n-#define TMP_RENAMED_LOG  \"refs/.tmp-renamed-log\"\n-\n-struct rename_cb {\n-\tconst char *tmp_renamed_log;\n-\tint true_errno;\n-};\n-\n-static int rename_tmp_log_callback(const char *path, void *cb_data)\n-{\n-\tstruct rename_cb *cb = cb_data;\n-\n-\tif (rename(cb->tmp_renamed_log, path)) {\n-\t\t/*\n-\t\t * rename(a, b) when b is an existing directory ought\n-\t\t * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.\n-\t\t * Sheesh. Record the true errno for error reporting,\n-\t\t * but report EISDIR to raceproof_create_file() so\n-\t\t * that it knows to retry.\n-\t\t */\n-\t\tcb->true_errno = errno;\n-\t\tif (errno == ENOTDIR)\n-\t\t\terrno = EISDIR;\n-\t\treturn -1;\n-\t} else {\n-\t\treturn 0;\n-\t}\n-}\n-\n-static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)\n-{\n-\tstruct strbuf path = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n-\tstruct rename_cb cb;\n-\tint ret;\n-\n-\tfiles_reflog_path(refs, &path, newrefname);\n-\tfiles_reflog_path(refs, &tmp, TMP_RENAMED_LOG);\n-\tcb.tmp_renamed_log = tmp.buf;\n-\tret = raceproof_create_file(refs, path.buf, rename_tmp_log_callback, &cb);\n-\tif (ret) {\n-\t\tif (errno == EISDIR)\n-\t\t\terror(\"directory not empty: %s\", path.buf);\n-\t\telse\n-\t\t\terror(\"unable to move logfile %s to %s: %s\",\n-\t\t\t      tmp.buf, path.buf,\n-\t\t\t      strerror(cb.true_errno));\n-\t}\n-\n-\tstrbuf_release(&path);\n-\tstrbuf_release(&tmp);\n-\treturn ret;\n-}\n-\n-static enum ref_transaction_error write_ref_to_lockfile(struct files_ref_store *refs,\n-\t\t\t\t\t\t\tstruct ref_lock *lock,\n-\t\t\t\t\t\t\tconst struct object_id *oid,\n-\t\t\t\t\t\t\tstruct strbuf *err);\n-static int commit_ref_update(struct files_ref_store *refs,\n-\t\t\t     struct ref_lock *lock,\n-\t\t\t     const struct object_id *oid, const char *logmsg,\n-\t\t\t     int flags,\n-\t\t\t     struct strbuf *err);\n-\n-/*\n- * Emit a better error message than lockfile.c's\n- * unable_to_lock_message() would in case there is a D/F conflict with\n- * another existing reference. If there would be a conflict, emit an error\n- * message and return false; otherwise, return true.\n- *\n- * Note that this function is not safe against all races with other\n- * processes, and that's not its job. We'll emit a more verbose error on D/f\n- * conflicts if we get past it into lock_ref_oid_basic().\n- */\n-static int refs_rename_ref_available(struct ref_store *refs,\n-\t\t\t      const char *old_refname,\n-\t\t\t      const char *new_refname)\n-{\n-\tstruct string_list skip = STRING_LIST_INIT_NODUP;\n-\tstruct strbuf err = STRBUF_INIT;\n-\tint ok;\n-\n-\tstring_list_insert(&skip, old_refname);\n-\tok = !refs_verify_refname_available(refs, new_refname,\n-\t\t\t\t\t    NULL, &skip, 0, &err);\n-\tif (!ok)\n-\t\terror(\"%s\", err.buf);\n-\n-\tstring_list_clear(&skip, 0);\n-\tstrbuf_release(&err);\n-\treturn ok;\n-}\n-\n-static int files_copy_or_rename_ref(struct ref_store *ref_store,\n-\t\t\t    const char *oldrefname, const char *newrefname,\n-\t\t\t    const char *logmsg, int copy)\n-{\n-\tstruct files_ref_store *refs =\n-\t\tfiles_downcast(ref_store, REF_STORE_WRITE, \"rename_ref\");\n-\tstruct object_id orig_oid;\n-\tint flag = 0, logmoved = 0;\n-\tstruct ref_lock *lock;\n-\tstruct stat loginfo;\n-\tstruct strbuf sb_oldref = STRBUF_INIT;\n-\tstruct strbuf sb_newref = STRBUF_INIT;\n-\tstruct strbuf tmp_renamed_log = STRBUF_INIT;\n-\tint log, ret;\n-\tstruct strbuf err = STRBUF_INIT;\n-\n-\tfiles_reflog_path(refs, &sb_oldref, oldrefname);\n-\tfiles_reflog_path(refs, &sb_newref, newrefname);\n-\tfiles_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);\n-\n-\tlog = !lstat(sb_oldref.buf, &loginfo);\n-\tif (log && S_ISLNK(loginfo.st_mode)) {\n-\t\tret = error(\"reflog for %s is a symlink\", oldrefname);\n-\t\tgoto out;\n-\t}\n-\n-\tif (!refs_resolve_ref_unsafe(&refs->base, oldrefname,\n-\t\t\t\t     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n-\t\t\t\t     &orig_oid, &flag)) {\n-\t\tret = error(\"refname %s not found\", oldrefname);\n-\t\tgoto out;\n-\t}\n-\n-\tif (flag & REF_ISSYMREF) {\n-\t\tif (copy)\n-\t\t\tret = error(\"refname %s is a symbolic ref, copying it is not supported\",\n-\t\t\t\t    oldrefname);\n-\t\telse\n-\t\t\tret = error(\"refname %s is a symbolic ref, renaming it is not supported\",\n-\t\t\t\t    oldrefname);\n-\t\tgoto out;\n-\t}\n-\tif (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {\n-\t\tret = 1;\n-\t\tgoto out;\n-\t}\n-\n-\tif (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {\n-\t\tret = error(\"unable to move logfile logs/%s to logs/\"TMP_RENAMED_LOG\": %s\",\n-\t\t\t    oldrefname, strerror(errno));\n-\t\tgoto out;\n-\t}\n-\n-\tif (copy && log && copy_file(refs->base.repo, tmp_renamed_log.buf, sb_oldref.buf, 0644)) {\n-\t\tret = error(\"unable to copy logfile logs/%s to logs/\"TMP_RENAMED_LOG\": %s\",\n-\t\t\t    oldrefname, strerror(errno));\n-\t\tgoto out;\n-\t}\n-\n-\tif (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,\n-\t\t\t    &orig_oid, REF_NO_DEREF)) {\n-\t\terror(\"unable to delete old %s\", oldrefname);\n-\t\tgoto rollback;\n-\t}\n-\n-\t/*\n-\t * Since we are doing a shallow lookup, oid is not the\n-\t * correct value to pass to delete_ref as old_oid. But that\n-\t * doesn't matter, because an old_oid check wouldn't add to\n-\t * the safety anyway; we want to delete the reference whatever\n-\t * its current value.\n-\t */\n-\tif (!copy && refs_resolve_ref_unsafe(&refs->base, newrefname,\n-\t\t\t\t\t     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n-\t\t\t\t\t     NULL, NULL) &&\n-\t    refs_delete_ref(&refs->base, NULL, newrefname,\n-\t\t\t    NULL, REF_NO_DEREF)) {\n-\t\tif (errno == EISDIR) {\n-\t\t\tstruct strbuf path = STRBUF_INIT;\n-\t\t\tint result;\n-\n-\t\t\tfiles_ref_path(refs, &path, newrefname);\n-\t\t\tresult = remove_empty_directories(&path);\n-\t\t\tstrbuf_release(&path);\n-\n-\t\t\tif (result) {\n-\t\t\t\terror(\"Directory not empty: %s\", newrefname);\n-\t\t\t\tgoto rollback;\n-\t\t\t}\n-\t\t} else {\n-\t\t\terror(\"unable to delete existing %s\", newrefname);\n-\t\t\tgoto rollback;\n-\t\t}\n-\t}\n-\n-\tif (log && rename_tmp_log(refs, newrefname))\n-\t\tgoto rollback;\n-\n-\tlogmoved = log;\n-\n-\tlock = lock_ref_oid_basic(refs, newrefname, &err);\n-\tif (!lock) {\n-\t\tif (copy)\n-\t\t\terror(\"unable to copy '%s' to '%s': %s\", oldrefname, newrefname, err.buf);\n-\t\telse\n-\t\t\terror(\"unable to rename '%s' to '%s': %s\", oldrefname, newrefname, err.buf);\n-\t\tstrbuf_release(&err);\n-\t\tgoto rollback;\n-\t}\n-\toidcpy(&lock->old_oid, &orig_oid);\n-\n-\tif (write_ref_to_lockfile(refs, lock, &orig_oid, &err) ||\n-\t    commit_ref_update(refs, lock, &orig_oid, logmsg, 0, &err)) {\n-\t\terror(\"unable to write current sha1 into %s: %s\", newrefname, err.buf);\n-\t\tstrbuf_release(&err);\n-\t\tgoto rollback;\n-\t}\n-\n-\tret = 0;\n-\tgoto out;\n-\n- rollback:\n-\tlock = lock_ref_oid_basic(refs, oldrefname, &err);\n-\tif (!lock) {\n-\t\terror(\"unable to lock %s for rollback: %s\", oldrefname, err.buf);\n-\t\tstrbuf_release(&err);\n-\t\tgoto rollbacklog;\n-\t}\n-\n-\tif (write_ref_to_lockfile(refs, lock, &orig_oid, &err) ||\n-\t    commit_ref_update(refs, lock, &orig_oid, NULL, REF_SKIP_CREATE_REFLOG, &err)) {\n-\t\terror(\"unable to write current sha1 into %s: %s\", oldrefname, err.buf);\n-\t\tstrbuf_release(&err);\n-\t}\n-\n- rollbacklog:\n-\tif (logmoved && rename(sb_newref.buf, sb_oldref.buf))\n-\t\terror(\"unable to restore logfile %s from %s: %s\",\n-\t\t\toldrefname, newrefname, strerror(errno));\n-\tif (!logmoved && log &&\n-\t    rename(tmp_renamed_log.buf, sb_oldref.buf))\n-\t\terror(\"unable to restore logfile %s from logs/\"TMP_RENAMED_LOG\": %s\",\n-\t\t\toldrefname, strerror(errno));\n-\tret = 1;\n- out:\n-\tstrbuf_release(&sb_newref);\n-\tstrbuf_release(&sb_oldref);\n-\tstrbuf_release(&tmp_renamed_log);\n-\n-\treturn ret;\n-}\n-\n-static int files_rename_ref(struct ref_store *ref_store,\n-\t\t\t    const char *oldrefname, const char *newrefname,\n-\t\t\t    const char *logmsg)\n-{\n-\treturn files_copy_or_rename_ref(ref_store, oldrefname,\n-\t\t\t\t newrefname, logmsg, 0);\n-}\n-\n-static int files_copy_ref(struct ref_store *ref_store,\n-\t\t\t    const char *oldrefname, const char *newrefname,\n-\t\t\t    const char *logmsg)\n-{\n-\treturn files_copy_or_rename_ref(ref_store, oldrefname,\n-\t\t\t\t newrefname, logmsg, 1);\n-}\n-\n static int close_ref_gently(struct ref_lock *lock)\n {\n \tif (close_lock_file_gently(&lock->lk))\n@@ -2121,71 +1854,6 @@ static enum ref_transaction_error write_ref_to_lockfile(struct files_ref_store *\n \treturn 0;\n }\n \n-/*\n- * Commit a change to a loose reference that has already been written\n- * to the loose reference lockfile. Also update the reflogs if\n- * necessary, using the specified lockmsg (which can be NULL).\n- */\n-static int commit_ref_update(struct files_ref_store *refs,\n-\t\t\t     struct ref_lock *lock,\n-\t\t\t     const struct object_id *oid, const char *logmsg,\n-\t\t\t     int flags,\n-\t\t\t     struct strbuf *err)\n-{\n-\tfiles_assert_main_repository(refs, \"commit_ref_update\");\n-\n-\tclear_loose_ref_cache(refs);\n-\tif (files_log_ref_write(refs, lock->ref_name, &lock->old_oid, oid, NULL,\n-\t\t\t\tlogmsg, flags, err)) {\n-\t\tchar *old_msg = strbuf_detach(err, NULL);\n-\t\tstrbuf_addf(err, \"cannot update the ref '%s': %s\",\n-\t\t\t    lock->ref_name, old_msg);\n-\t\tfree(old_msg);\n-\t\tunlock_ref(lock);\n-\t\treturn -1;\n-\t}\n-\n-\tif (strcmp(lock->ref_name, \"HEAD\") != 0) {\n-\t\t/*\n-\t\t * Special hack: If a branch is updated directly and HEAD\n-\t\t * points to it (may happen on the remote side of a push\n-\t\t * for example) then logically the HEAD reflog should be\n-\t\t * updated too.\n-\t\t * A generic solution implies reverse symref information,\n-\t\t * but finding all symrefs pointing to the given branch\n-\t\t * would be rather costly for this rare event (the direct\n-\t\t * update of a branch) to be worth it.  So let's cheat and\n-\t\t * check with HEAD only which should cover 99% of all usage\n-\t\t * scenarios (even 100% of the default ones).\n-\t\t */\n-\t\tint head_flag;\n-\t\tconst char *head_ref;\n-\n-\t\thead_ref = refs_resolve_ref_unsafe(&refs->base, \"HEAD\",\n-\t\t\t\t\t\t   RESOLVE_REF_READING,\n-\t\t\t\t\t\t   NULL, &head_flag);\n-\t\tif (head_ref && (head_flag & REF_ISSYMREF) &&\n-\t\t    !strcmp(head_ref, lock->ref_name)) {\n-\t\t\tstruct strbuf log_err = STRBUF_INIT;\n-\t\t\tif (files_log_ref_write(refs, \"HEAD\", &lock->old_oid,\n-\t\t\t\t\t\toid, NULL, logmsg, flags,\n-\t\t\t\t\t\t&log_err)) {\n-\t\t\t\terror(\"%s\", log_err.buf);\n-\t\t\t\tstrbuf_release(&log_err);\n-\t\t\t}\n-\t\t}\n-\t}\n-\n-\tif (commit_ref(lock)) {\n-\t\tstrbuf_addf(err, \"couldn't set '%s'\", lock->ref_name);\n-\t\tunlock_ref(lock);\n-\t\treturn -1;\n-\t}\n-\n-\tunlock_ref(lock);\n-\treturn 0;\n-}\n-\n #if defined(NO_SYMLINK_HEAD) || defined(WITH_BREAKING_CHANGES)\n #define create_ref_symlink(a, b) (-1)\n #else\n@@ -4468,8 +4136,6 @@ struct ref_storage_be refs_be_files = {\n \n \t.optimize = files_optimize,\n \t.optimize_required = files_optimize_required,\n-\t.rename_ref = files_rename_ref,\n-\t.copy_ref = files_copy_ref,\n \n \t.iterator_begin = files_ref_iterator_begin,\n \t.read_raw_ref = files_read_raw_ref,\ndiff --git a/refs/packed-backend.c b/refs/packed-backend.c\nindex a73fc6aca7..364a912912 100644\n--- a/refs/packed-backend.c\n+++ b/refs/packed-backend.c\n@@ -2164,8 +2164,6 @@ struct ref_storage_be refs_be_packed = {\n \t.optimize = packed_optimize,\n \t.optimize_required = packed_optimize_required,\n \n-\t.rename_ref = NULL,\n-\t.copy_ref = NULL,\n \n \t.iterator_begin = packed_ref_iterator_begin,\n \t.read_raw_ref = packed_read_raw_ref,\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 2746ce5b49..c415ba0a4e 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -470,13 +470,6 @@ typedef int optimize_required_fn(struct ref_store *ref_store,\n \t\t\t\t struct refs_optimize_opts *opts,\n \t\t\t\t bool *required);\n \n-typedef int rename_ref_fn(struct ref_store *ref_store,\n-\t\t\t  const char *oldref, const char *newref,\n-\t\t\t  const char *logmsg);\n-typedef int copy_ref_fn(struct ref_store *ref_store,\n-\t\t\t  const char *oldref, const char *newref,\n-\t\t\t  const char *logmsg);\n-\n /*\n  * Iterate over the references in `ref_store` whose names start with\n  * `prefix`. `prefix` is matched as a literal string, without regard\n@@ -596,8 +589,6 @@ struct ref_storage_be {\n \n \toptimize_fn *optimize;\n \toptimize_required_fn *optimize_required;\n-\trename_ref_fn *rename_ref;\n-\tcopy_ref_fn *copy_ref;\n \n \tref_iterator_begin_fn *iterator_begin;\n \tread_raw_ref_fn *read_raw_ref;\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 72c1596c11..ef3bab69ee 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1798,290 +1798,6 @@ struct write_create_symref_arg {\n \tconst char *logmsg;\n };\n \n-struct write_copy_arg {\n-\tstruct reftable_ref_store *refs;\n-\tstruct reftable_backend *be;\n-\tconst char *oldname;\n-\tconst char *newname;\n-\tconst char *logmsg;\n-\tint delete_old;\n-};\n-\n-static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n-{\n-\tstruct write_copy_arg *arg = cb_data;\n-\tuint64_t deletion_ts, creation_ts;\n-\tstruct reftable_ref_record old_ref = {0}, refs[2] = {0};\n-\tstruct reftable_log_record old_log = {0}, *logs = NULL;\n-\tstruct reftable_iterator it = {0};\n-\tstruct string_list skip = STRING_LIST_INIT_NODUP;\n-\tstruct ident_split committer_ident = {0};\n-\tstruct strbuf errbuf = STRBUF_INIT;\n-\tsize_t logs_nr = 0, logs_alloc = 0, i;\n-\tconst char *committer_info;\n-\tint ret;\n-\n-\tcommitter_info = git_committer_info(0);\n-\tif (split_ident_line(&committer_ident, committer_info, strlen(committer_info)))\n-\t\tBUG(\"failed splitting committer info\");\n-\n-\tif (reftable_stack_read_ref(arg->be->stack, arg->oldname, &old_ref)) {\n-\t\tret = error(_(\"refname %s not found\"), arg->oldname);\n-\t\tgoto done;\n-\t}\n-\tif (old_ref.value_type == REFTABLE_REF_SYMREF) {\n-\t\tret = error(_(\"refname %s is a symbolic ref, copying it is not supported\"),\n-\t\t\t    arg->oldname);\n-\t\tgoto done;\n-\t}\n-\n-\t/*\n-\t * There's nothing to do in case the old and new name are the same, so\n-\t * we exit early in that case.\n-\t */\n-\tif (!strcmp(arg->oldname, arg->newname)) {\n-\t\tret = 0;\n-\t\tgoto done;\n-\t}\n-\n-\t/*\n-\t * Verify that the new refname is available.\n-\t */\n-\tif (arg->delete_old)\n-\t\tstring_list_insert(&skip, arg->oldname);\n-\tret = refs_verify_refname_available(&arg->refs->base, arg->newname,\n-\t\t\t\t\t    NULL, &skip, 0, &errbuf);\n-\tif (ret < 0) {\n-\t\terror(\"%s\", errbuf.buf);\n-\t\tgoto done;\n-\t}\n-\n-\t/*\n-\t * When deleting the old reference we have to use two update indices:\n-\t * once to delete the old ref and its reflog, and once to create the\n-\t * new ref and its reflog. They need to be staged with two separate\n-\t * indices because the new reflog needs to encode both the deletion of\n-\t * the old branch and the creation of the new branch, and we cannot do\n-\t * two changes to a reflog in a single update.\n-\t */\n-\tdeletion_ts = creation_ts = reftable_stack_next_update_index(arg->be->stack);\n-\tif (arg->delete_old)\n-\t\tcreation_ts++;\n-\tret = reftable_writer_set_limits(writer, deletion_ts, creation_ts);\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-\t/*\n-\t * Add the new reference. If this is a rename then we also delete the\n-\t * old reference.\n-\t */\n-\trefs[0] = old_ref;\n-\trefs[0].refname = xstrdup(arg->newname);\n-\trefs[0].update_index = creation_ts;\n-\tif (arg->delete_old) {\n-\t\trefs[1].refname = xstrdup(arg->oldname);\n-\t\trefs[1].value_type = REFTABLE_REF_DELETION;\n-\t\trefs[1].update_index = deletion_ts;\n-\t}\n-\tret = reftable_writer_add_refs(writer, refs, arg->delete_old ? 2 : 1);\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-\t/*\n-\t * When deleting the old branch we need to create a reflog entry on the\n-\t * new branch name that indicates that the old branch has been deleted\n-\t * and then recreated. This is a tad weird, but matches what the files\n-\t * backend does.\n-\t */\n-\tif (arg->delete_old) {\n-\t\tstruct strbuf head_referent = STRBUF_INIT;\n-\t\tstruct object_id head_oid;\n-\t\tint append_head_reflog;\n-\t\tunsigned head_type = 0;\n-\n-\t\tALLOC_GROW(logs, logs_nr + 1, logs_alloc);\n-\t\tmemset(&logs[logs_nr], 0, sizeof(logs[logs_nr]));\n-\t\tfill_reftable_log_record(&logs[logs_nr], &committer_ident);\n-\t\tlogs[logs_nr].refname = xstrdup(arg->newname);\n-\t\tlogs[logs_nr].update_index = deletion_ts;\n-\t\tlogs[logs_nr].value.update.message =\n-\t\t\txstrndup(arg->logmsg, reftable_be_write_options(arg->refs)->opts.block_size / 2);\n-\t\tmemcpy(logs[logs_nr].value.update.old_hash, old_ref.value.val1, GIT_MAX_RAWSZ);\n-\t\tlogs_nr++;\n-\n-\t\tret = reftable_backend_read_ref(arg->be, \"HEAD\", &head_oid,\n-\t\t\t\t\t\t&head_referent, &head_type);\n-\t\tif (ret < 0)\n-\t\t\tgoto done;\n-\t\tappend_head_reflog = (head_type & REF_ISSYMREF) && !strcmp(head_referent.buf, arg->oldname);\n-\t\tstrbuf_release(&head_referent);\n-\n-\t\t/*\n-\t\t * The files backend uses `refs_delete_ref()` to delete the old\n-\t\t * branch name, which will append a reflog entry for HEAD in\n-\t\t * case it points to the old branch.\n-\t\t */\n-\t\tif (append_head_reflog) {\n-\t\t\tALLOC_GROW(logs, logs_nr + 1, logs_alloc);\n-\t\t\tlogs[logs_nr] = logs[logs_nr - 1];\n-\t\t\tlogs[logs_nr].refname = xstrdup(\"HEAD\");\n-\t\t\tlogs[logs_nr].value.update.name =\n-\t\t\t\txstrdup(logs[logs_nr].value.update.name);\n-\t\t\tlogs[logs_nr].value.update.email =\n-\t\t\t\txstrdup(logs[logs_nr].value.update.email);\n-\t\t\tlogs[logs_nr].value.update.message =\n-\t\t\t\txstrdup(logs[logs_nr].value.update.message);\n-\t\t\tlogs_nr++;\n-\t\t}\n-\t}\n-\n-\t/*\n-\t * Create the reflog entry for the newly created branch.\n-\t */\n-\tALLOC_GROW(logs, logs_nr + 1, logs_alloc);\n-\tmemset(&logs[logs_nr], 0, sizeof(logs[logs_nr]));\n-\tfill_reftable_log_record(&logs[logs_nr], &committer_ident);\n-\tlogs[logs_nr].refname = xstrdup(arg->newname);\n-\tlogs[logs_nr].update_index = creation_ts;\n-\tlogs[logs_nr].value.update.message =\n-\t\txstrndup(arg->logmsg, reftable_be_write_options(arg->refs)->opts.block_size / 2);\n-\tmemcpy(logs[logs_nr].value.update.new_hash, old_ref.value.val1, GIT_MAX_RAWSZ);\n-\tlogs_nr++;\n-\n-\t/*\n-\t * In addition to writing the reflog entry for the new branch, we also\n-\t * copy over all log entries from the old reflog. Last but not least,\n-\t * when renaming we also have to delete all the old reflog entries.\n-\t */\n-\tret = reftable_stack_init_log_iterator(arg->be->stack, &it);\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-\tret = reftable_iterator_seek_log(&it, arg->oldname);\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-\twhile (1) {\n-\t\tret = reftable_iterator_next_log(&it, &old_log);\n-\t\tif (ret < 0)\n-\t\t\tgoto done;\n-\t\tif (ret > 0 || strcmp(old_log.refname, arg->oldname)) {\n-\t\t\tret = 0;\n-\t\t\tbreak;\n-\t\t}\n-\t\tif (reftable_log_record_is_deletion(&old_log))\n-\t\t\tcontinue;\n-\n-\t\tfree(old_log.refname);\n-\n-\t\t/*\n-\t\t * Copy over the old reflog entry with the new refname.\n-\t\t */\n-\t\tALLOC_GROW(logs, logs_nr + 1, logs_alloc);\n-\t\tlogs[logs_nr] = old_log;\n-\t\tlogs[logs_nr].refname = xstrdup(arg->newname);\n-\t\tlogs_nr++;\n-\n-\t\t/*\n-\t\t * Delete the old reflog entry in case we are renaming.\n-\t\t */\n-\t\tif (arg->delete_old) {\n-\t\t\tALLOC_GROW(logs, logs_nr + 1, logs_alloc);\n-\t\t\tmemset(&logs[logs_nr], 0, sizeof(logs[logs_nr]));\n-\t\t\tlogs[logs_nr].refname = xstrdup(arg->oldname);\n-\t\t\tlogs[logs_nr].value_type = REFTABLE_LOG_DELETION;\n-\t\t\tlogs[logs_nr].update_index = old_log.update_index;\n-\t\t\tlogs_nr++;\n-\t\t}\n-\n-\t\t/*\n-\t\t * Transfer ownership of the log record we're iterating over to\n-\t\t * the array of log records. Otherwise, the pointers would get\n-\t\t * free'd or reallocated by the iterator.\n-\t\t */\n-\t\tmemset(&old_log, 0, sizeof(old_log));\n-\t}\n-\n-\tret = reftable_writer_add_logs(writer, logs, logs_nr);\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-done:\n-\tassert(ret != REFTABLE_API_ERROR);\n-\treftable_iterator_destroy(&it);\n-\tstring_list_clear(&skip, 0);\n-\tstrbuf_release(&errbuf);\n-\tfor (i = 0; i < logs_nr; i++)\n-\t\treftable_log_record_release(&logs[i]);\n-\tfree(logs);\n-\tfor (i = 0; i < ARRAY_SIZE(refs); i++)\n-\t\treftable_ref_record_release(&refs[i]);\n-\treftable_ref_record_release(&old_ref);\n-\treftable_log_record_release(&old_log);\n-\treturn ret;\n-}\n-\n-static int reftable_be_rename_ref(struct ref_store *ref_store,\n-\t\t\t\t  const char *oldrefname,\n-\t\t\t\t  const char *newrefname,\n-\t\t\t\t  const char *logmsg)\n-{\n-\tstruct reftable_ref_store *refs =\n-\t\treftable_be_downcast(ref_store, REF_STORE_WRITE, \"rename_ref\");\n-\tstruct write_copy_arg arg = {\n-\t\t.refs = refs,\n-\t\t.oldname = oldrefname,\n-\t\t.newname = newrefname,\n-\t\t.logmsg = logmsg,\n-\t\t.delete_old = 1,\n-\t};\n-\tint ret;\n-\n-\tret = refs->err;\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-\tret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);\n-\tif (ret)\n-\t\tgoto done;\n-\tret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,\n-\t\t\t\t &reftable_be_write_options(refs)->opts);\n-\n-done:\n-\tassert(ret != REFTABLE_API_ERROR);\n-\treturn ret;\n-}\n-\n-static int reftable_be_copy_ref(struct ref_store *ref_store,\n-\t\t\t\tconst char *oldrefname,\n-\t\t\t\tconst char *newrefname,\n-\t\t\t\tconst char *logmsg)\n-{\n-\tstruct reftable_ref_store *refs =\n-\t\treftable_be_downcast(ref_store, REF_STORE_WRITE, \"copy_ref\");\n-\tstruct write_copy_arg arg = {\n-\t\t.refs = refs,\n-\t\t.oldname = oldrefname,\n-\t\t.newname = newrefname,\n-\t\t.logmsg = logmsg,\n-\t};\n-\tint ret;\n-\n-\tret = refs->err;\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-\tret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);\n-\tif (ret)\n-\t\tgoto done;\n-\tret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,\n-\t\t\t\t &reftable_be_write_options(refs)->opts);\n-\n-done:\n-\tassert(ret != REFTABLE_API_ERROR);\n-\treturn ret;\n-}\n-\n struct reftable_reflog_iterator {\n \tstruct ref_iterator base;\n \tstruct reftable_ref_store *refs;\n@@ -2909,9 +2625,6 @@ struct ref_storage_be refs_be_reftable = {\n \t.optimize = reftable_be_optimize,\n \t.optimize_required = reftable_be_optimize_required,\n \n-\t.rename_ref = reftable_be_rename_ref,\n-\t.copy_ref = reftable_be_copy_ref,\n-\n \t.iterator_begin = reftable_be_iterator_begin,\n \t.read_raw_ref = reftable_be_read_raw_ref,\n \t.read_symbolic_ref = reftable_be_read_symbolic_ref,\n-- \n2.39.3 (Apple Git-146)\n\n\n"},{"id":"554418","messageId":"xmqq8q4970ah.fsf@gitster.g","threadId":"66350","inReplyTo":"cover.1791395643.git.maciej.ciemborowicz@gmail.com","subject":"Re: [PATCH v3 0/4] refs: run copy and rename through transactions","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-07T19:55:50Z","receivedAt":"2026-10-07T19:55:50Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com> writes:\n\n> Reference copy and rename bypass the transaction API. With files, the\n> reference-transaction hook sees only the source deletion; with reftable,\n> it sees neither endpoint. This series puts the logical ref updates and\n> the reflog history into ordinary transactions so hooks can observe and\n> reject the complete operation.\n> ...\n>  Documentation/githooks.adoc     |  10 +\n>  refs.c                          | 200 ++++++++-\n>  refs.h                          |  25 ++\n>  refs/debug.c                    |  24 --\n>  refs/files-backend.c            | 739 +++++++++++++++++---------------\n>  refs/packed-backend.c           |   2 -\n>  refs/refs-internal.h            |  28 +-\n>  refs/reftable-backend.c         | 334 ++-------------\n>  t/helper/test-ref-store.c       |  76 ++++\n>  t/perf/p1424-ref-copy-rename.sh |  48 +++\n>  t/t1424-ref-copy-transaction.sh | 352 +++++++++++++++\n>  t/t1425-reflog-transaction.sh   |  59 +++\n>  12 files changed, 1206 insertions(+), 691 deletions(-)\n>  create mode 100755 t/perf/p1424-ref-copy-rename.sh\n>  create mode 100755 t/t1424-ref-copy-transaction.sh\n>  create mode 100755 t/t1425-reflog-transaction.sh\n>\n> Range-diff against v2:\n> 1:  d852537d8c < -:  ---------- refs: run copy and rename through transactions\n> -:  ---------- > 1:  6d7c146e57 refs: distinguish internal transactions from logical updates\n> -:  ---------- > 2:  5c3ec4eb49 refs: support replacing reflogs in a transaction\n> -:  ---------- > 3:  77af4e809c refs: run copy and rename through ordinary transactions\n> -:  ---------- > 4:  83fa644fb3 refs: remove backend-specific copy and rename callbacks\n\nI do not think I have time to read these humoungous patches, some of\nwhich weigh more than 1000+ lines, with fine toothed comb any time\nsoon, but when applied to the recent tip of master 6de20f6092 (The\n4th batch, 2026-10-06), it seems to break t0600 and t5510.  The\nfailing tests do not seem to be anything so system specific (and I\nam on a GNU/Linux platform that is not anything remarkable).\n\nI wonder what I am doing differently.  Did they pass for you?  Here\nare the first failure from these test scripts.\n\nThanks.\n\nexpecting success of 0600.16 'delete fails cleanly if packed-refs.new write fails':\n        # Setup and expectations are similar to the test above.\n        prefix=refs/failed-packed-refs &&\n        git update-ref $prefix/foo $C &&\n        git pack-refs --all &&\n        git update-ref $prefix/foo $D &&\n        git for-each-ref $prefix >unchanged &&\n        # This should not happen in practice, but it is an easy way to get a\n        # reliable error (we open with create_tempfile(), which uses O_EXCL).\n        : >.git/packed-refs.new &&\n        test_when_finished \"rm -f .git/packed-refs.new\" &&\n        test_must_fail git update-ref -d $prefix/foo &&\n        git for-each-ref $prefix >actual &&\n        test_cmp unchanged actual\n\ntest_must_fail: command succeeded: git update-ref -d refs/failed-packed-refs/foo\nnot ok 16 - delete fails cleanly if packed-refs.new write fails\n\nexpecting success of 5510.35 'fetch --prune fails to delete branches':\n        git clone . prune-fail &&\n        (\n                cd prune-fail &&\n                git update-ref refs/remotes/origin/extrabranch main &&\n                git pack-refs --all &&\n                : this will prevent --prune from locking packed-refs for deleting refs, but adding loose refs still succeeds  &&\n                >.git/packed-refs.new &&\n\n                test_must_fail git fetch --prune origin\n        )\n\nCloning into 'prune-fail'...\ndone.\nFrom /usr/local/google/home/jch/w/git.git/t/trash directory.t5510-fetch/.\n - [deleted]         (none)     -> origin/extrabranch\ntest_must_fail: command succeeded: git fetch --prune origin\nnot ok 35 - fetch --prune fails to delete branches\n\n\n\n"},{"id":"554462","messageId":"cover.1791452597.git.maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"20260920165037.88524-1-maciej.ciemborowicz@gmail.com","subject":"[PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-08T09:44:15Z","receivedAt":"2026-10-08T09:44:15Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"Reference copy and rename bypass the transaction API. With files, the\nreference-transaction hook sees only the source deletion; with reftable,\nit sees neither endpoint. This series represents the logical ref updates\nand reflog history through ordinary transactions, allowing hooks to\nobserve and reject the operation.\n\nJunio, thank you for reporting the t0600 and t5510 failures in v3. I\nreproduced both on macOS with the original base as well. I had not run\nthose suites before submitting v3; they were outside the test selection\nreported in that cover letter. That was a gap in my validation, and I\nam sorry for sending the regression.\n\nPatch 2 added a call to prepare_reflog_replacements() after preparing the\npacked transaction. If packed preparation failed, its error was saved in\nret, but the new call overwrote it with success. The files transaction\ncould then proceed despite the failure to prepare packed-refs. The fix\njumps to cleanup after freeing the failed packed transaction, preserving\nthe error and releasing the remaining resources. The existing t0600.16\nand t5510.35 cover this path; their expectations have not been changed.\n\nChanges since v3:\n\n* Rebase onto 6de20f6092 (The 4th batch, 2026-10-06), the master commit\n  used in Junio's report.\n* Preserve the packed preparation error in patch 2 as described above.\n* Register t1425 and t1424 in t/meson.build in the commits adding them.\n\nThe four-patch structure and implementation otherwise remain unchanged.\nThis iteration does not address the concern about the size of the main\npatch; the range-diff below isolates the correction and registrations.\n\nI ran the complete standard core test collection, including the unit-test\nexecutable, on macOS/arm64 with each default ref backend:\n\n  files:    Files=1064, Tests=34710, Result: PASS\n  reftable: Files=1064, Tests=34712, Result: PASS\n\nBoth full runs include successful t0600, t5510, t1424 and t1425 runs.\nThe build enables Perl, Python, cURL and gettext. The files run used:\n\n  LC_ALL=C make -j8 PYTHON_PATH=/opt/homebrew/bin/python3 \\\n    GNU_GETTEXT_PATH=/opt/homebrew/opt/gettext \\\n    DEFAULT_TEST_TARGET=prove GIT_PROVE_OPTS='--jobs 8' test\n\nThe reftable run used GIT_TEST_DEFAULT_REF_FORMAT=reftable and ran every\nt[0-9][0-9][0-9][0-9]-*.sh plus unit-tests/bin/unit-tests through prove,\nwith four jobs and a separate TEST_OUTPUT_DIRECTORY. Backend-specific\nsuites can override the default. The Meson registration check and\ngit diff --check also pass. The contrib test target completed with no\nunexpected failures; diff-highlight retains two known TODO failures.\n\nThe harness totals include skips and expected failures. The files run\nskipped 126 whole scripts, mostly for unavailable SVN, Perforce and CVS\ntools. Other skips include GPG, JGit, Windows-specific tests, tests\nrequiring sudo or writable /, case-sensitive filesystem tests, the\nopt-in 2GB clone test, and t5564 because its web-server setup failed.\nThere are also individual prerequisite-based skips within suites.\nI have not validated this iteration on Linux or Windows.\n\nThe performance trade-off described in v3 remains: replaying history\nuses O(N) time and memory, replacing the files backend's constant-time\nreflog rename. The measurements in patch 3 are from the v3 base, not a\nnew benchmark on this base. Files transactions also remain non-atomic\nwith respect to process crashes and failures late in finish.\n\nPrevious iteration:\nhttps://lore.kernel.org/git/cover.1791395643.git.maciej.ciemborowicz@gmail.com/\n\nMaciej Ciemborowicz (4):\n  refs: distinguish internal transactions from logical updates\n  refs: support replacing reflogs in a transaction\n  refs: run copy and rename through ordinary transactions\n  refs: remove backend-specific copy and rename callbacks\n\n Documentation/githooks.adoc     |  10 +\n refs.c                          | 200 ++++++++-\n refs.h                          |  25 ++\n refs/debug.c                    |  24 --\n refs/files-backend.c            | 740 +++++++++++++++++---------------\n refs/packed-backend.c           |   2 -\n refs/refs-internal.h            |  28 +-\n refs/reftable-backend.c         | 334 ++------------\n t/helper/test-ref-store.c       |  76 ++++\n t/meson.build                   |   2 +\n t/perf/p1424-ref-copy-rename.sh |  48 +++\n t/t1424-ref-copy-transaction.sh | 352 +++++++++++++++\n t/t1425-reflog-transaction.sh   |  59 +++\n 13 files changed, 1209 insertions(+), 691 deletions(-)\n create mode 100755 t/perf/p1424-ref-copy-rename.sh\n create mode 100755 t/t1424-ref-copy-transaction.sh\n create mode 100755 t/t1425-reflog-transaction.sh\n\nRange-diff against v3:\n1:  6d7c146e57 = 1:  948927d8fb refs: distinguish internal transactions from logical updates\n2:  5c3ec4eb49 ! 2:  d023da3c09 refs: support replacing reflogs in a transaction\n    @@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref\n      \ttransaction->backend_data = backend_data;\n      \n      \t/*\n    +@@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref_store,\n    + \t\t\tif (ret) {\n    + \t\t\t\tref_transaction_free(packed_transaction);\n    + \t\t\t\tbackend_data->packed_transaction = NULL;\n    ++\t\t\t\tgoto cleanup;\n    + \t\t\t}\n    + \t\t} else {\n    + \t\t\t/*\n     @@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref_store,\n      \t\t}\n      \t}\n    @@ t/helper/test-ref-store.c: static struct command commands[] = {\n      \t{ \"for-each-ref--exclude\", cmd_for_each_ref__exclude },\n      \t{ \"resolve-ref\", cmd_resolve_ref },\n     \n    + ## t/meson.build ##\n    +@@ t/meson.build: integration_tests = [\n    +   't1421-reflog-write.sh',\n    +   't1422-show-ref-exists.sh',\n    +   't1423-ref-backend.sh',\n    ++  't1425-reflog-transaction.sh',\n    +   't1430-bad-ref-name.sh',\n    +   't1450-fsck.sh',\n    +   't1451-fsck-buffer.sh',\n    +\n      ## t/t1425-reflog-transaction.sh (new) ##\n     @@\n     +#!/bin/sh\n3:  77af4e809c ! 3:  150349f9d0 refs: run copy and rename through ordinary transactions\n    @@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref\n     -\t\tif (update->flags & REF_DELETING &&\n     +\t\tif (update->flags & (REF_DELETING | REF_NEEDS_PACK) &&\n      \t\t    !(update->flags & REF_LOG_ONLY) &&\n    - \t\t    !(update->flags & REF_IS_PRUNING)) {\n    - \t\t\t/*\n    + \t\t    !(update->flags & REF_IS_PRUNING) &&\n    + \t\t    !is_root_ref(update->refname)) {\n     @@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref_store,\n      \t\t\t\t\tREF_HAVE_NEW | REF_NO_DEREF,\n      \t\t\t\t\t&update->new_oid, NULL, NULL,\n    @@ t/helper/test-ref-store.c: static struct command commands[] = {\n      \t{ \"for-each-ref--exclude\", cmd_for_each_ref__exclude },\n      \t{ \"resolve-ref\", cmd_resolve_ref },\n     \n    + ## t/meson.build ##\n    +@@ t/meson.build: integration_tests = [\n    +   't1421-reflog-write.sh',\n    +   't1422-show-ref-exists.sh',\n    +   't1423-ref-backend.sh',\n    ++  't1424-ref-copy-transaction.sh',\n    +   't1425-reflog-transaction.sh',\n    +   't1430-bad-ref-name.sh',\n    +   't1450-fsck.sh',\n    +\n      ## t/perf/p1424-ref-copy-rename.sh (new) ##\n     @@\n     +#!/bin/sh\n4:  83fa644fb3 = 4:  f3f2c7ee11 refs: remove backend-specific copy and rename callbacks\n\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\n-- \n2.39.3 (Apple Git-146)\n\n"},{"id":"554463","messageId":"948927d8fb4ca94742294d4f423ee1cb9819a7ae.1791452597.git.maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"cover.1791452597.git.maciej.ciemborowicz@gmail.com","subject":"[PATCH v4 1/4] refs: distinguish internal transactions from logical updates","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-08T09:44:16Z","receivedAt":"2026-10-08T09:44:16Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"A ref backend may use a nested transaction to persist part of a logical\nupdate. Provide an internal-only transaction flag so that these physical\nupdates can avoid reporting the same operation to reference-transaction\nhooks again.\n\nKeep the check in run_transaction_hook(), covering every hook state in\none place. The flag is deliberately not part of the public refs API.\nSubsequent changes use it for packed-refs updates belonging to a copy or\nrename.\n\nSigned-off-by: Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com>\n---\n refs.c               | 3 +++\n refs/refs-internal.h | 3 +++\n 2 files changed, 6 insertions(+)\n\ndiff --git a/refs.c b/refs.c\nindex 951db56113..642f895b71 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -2689,6 +2689,9 @@ static int run_transaction_hook(struct ref_transaction *transaction,\n \tstruct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;\n \tint ret = 0;\n \n+\tif (transaction->flags & REF_TRANSACTION_FLAG_INTERNAL)\n+\t\treturn 0;\n+\n \tstrvec_push(&opt.args, state);\n \n \topt.feed_pipe = transaction_hook_feed_stdin;\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex c3ac7b556f..0b41f5fa4b 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -8,6 +8,9 @@\n struct fsck_options;\n struct ref_transaction;\n \n+/* Physical updates nested in a transaction already reported to hooks. */\n+#define REF_TRANSACTION_FLAG_INTERNAL (1 << 2)\n+\n /*\n  * Data structures and functions for the internal use of the refs\n  * module. Code outside of the refs module should use only the public\n-- \n2.39.3 (Apple Git-146)\n\n\n"},{"id":"554464","messageId":"d023da3c09ff9f9f54ecbc6f66aa429f49581f17.1791452597.git.maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"cover.1791452597.git.maciej.ciemborowicz@gmail.com","subject":"[PATCH v4 2/4] refs: support replacing reflogs in a transaction","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-08T09:44:17Z","receivedAt":"2026-10-08T09:44:17Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"Explicit log-only updates can already recreate a reflog during\nmigration. Add ref_transaction_replace_reflog() to replace existing\nhistory with these entries without changing the reference itself. Keep\nits marker flag private to the refs implementation so callers cannot\nattach this cross-update behavior to an ordinary ref update\naccidentally.\n\nStage files-backend replacements in unique temporary files beside the\nlogs and install them only during finish. Buffer writes and order\nentries by their requested index. Aborting prepare only removes staging\nfiles. In reftable, tombstone the previous entries in the same table as\nthe replacement.\n\nDo not treat a log-only entry with a null new OID as a reference\ndeletion: it is part of the history being replayed. Propagate read\nerrors instead of silently copying a partial log.\n\nTest preservation of null-OID entries, ordering, removal, hook rejection\nand duplicate replacement. This is preparation for copying and renaming\nthrough the ordinary transaction path.\n\nSigned-off-by: Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com>\n---\n refs.c                        |  34 ++++++-\n refs.h                        |   9 ++\n refs/files-backend.c          | 181 +++++++++++++++++++++++++++++++---\n refs/refs-internal.h          |   3 +\n refs/reftable-backend.c       |  10 +-\n t/helper/test-ref-store.c     |  40 ++++++++\n t/meson.build                 |   1 +\n t/t1425-reflog-transaction.sh |  59 +++++++++++\n 8 files changed, 322 insertions(+), 15 deletions(-)\n create mode 100755 t/t1425-reflog-transaction.sh\n\ndiff --git a/refs.c b/refs.c\nindex 642f895b71..c02da9d966 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1436,7 +1436,6 @@ enum ref_transaction_error ref_transaction_update(struct ref_transaction *transa\n \t\tstrbuf_addstr(err, _(\"refusing to force and skip creation of reflog\"));\n \t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \t}\n-\n \tif (!transaction_refname_valid(refname, new_oid, flags, err))\n \t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \n@@ -1519,6 +1518,39 @@ int ref_transaction_update_reflog(struct ref_transaction *transaction,\n \treturn 0;\n }\n \n+int ref_transaction_replace_reflog(struct ref_transaction *transaction,\n+\t\t\t\t    const char *refname,\n+\t\t\t\t    struct strbuf *err)\n+{\n+\tsize_t i;\n+\tunsigned int flags = REF_LOG_ONLY | REF_NO_DEREF |\n+\t\tREF_SKIP_CREATE_REFLOG | REF_REPLACE_REFLOG;\n+\n+\tassert(err);\n+\tif (transaction->flags &\n+\t    (REF_TRANSACTION_FLAG_INITIAL | REF_TRANSACTION_ALLOW_FAILURE)) {\n+\t\tstrbuf_addstr(err, _(\"reflog replacement requires an \"\n+\t\t\t\t     \"all-or-nothing, non-initial transaction\"));\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n+\t}\n+\tif (!transaction_refname_valid(refname, NULL, flags, err))\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\n+\t\tif ((update->flags & REF_REPLACE_REFLOG) &&\n+\t\t    !strcmp(update->refname, refname)) {\n+\t\t\tstrbuf_addf(err, _(\"reflog replacement for '%s' already queued\"),\n+\t\t\t\t    refname);\n+\t\t\treturn REF_TRANSACTION_ERROR_NAME_CONFLICT;\n+\t\t}\n+\t}\n+\n+\tref_transaction_add_update(transaction, refname, flags,\n+\t\t\t\t   NULL, NULL, NULL, NULL, NULL, NULL, NULL);\n+\treturn 0;\n+}\n+\n int ref_transaction_create(struct ref_transaction *transaction,\n \t\t\t   const char *refname,\n \t\t\t   const struct object_id *new_oid,\ndiff --git a/refs.h b/refs.h\nindex ee3b8a62ef..b0b2b0e4fd 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -943,6 +943,15 @@ int ref_transaction_update_reflog(struct ref_transaction *transaction,\n \t\t\t\t  uint64_t index,\n \t\t\t\t  struct strbuf *err);\n \n+/*\n+ * Replace a reference's reflog with the explicit entries added to the same\n+ * transaction via ref_transaction_update_reflog(). Without such entries,\n+ * remove the reflog. This operation does not change the reference itself.\n+ */\n+int ref_transaction_replace_reflog(struct ref_transaction *transaction,\n+\t\t\t\t    const char *refname,\n+\t\t\t\t    struct strbuf *err);\n+\n /*\n  * Add a reference creation to transaction. new_oid is the value that\n  * the reference should have after the update; it must not be\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 7ddf0bef7d..36ecd21ed7 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1983,6 +1983,22 @@ static int files_create_reflog(struct ref_store *ref_store, const char *refname,\n \treturn 0;\n }\n \n+static void format_reflog_entry(struct strbuf *sb,\n+\t\t\t\tconst struct object_id *old_oid,\n+\t\t\t\tconst struct object_id *new_oid,\n+\t\t\t\tconst char *committer, const char *msg)\n+{\n+\tif (!committer)\n+\t\tcommitter = git_committer_info(0);\n+\n+\tstrbuf_addf(sb, \"%s %s %s\", oid_to_hex(old_oid), oid_to_hex(new_oid), committer);\n+\tif (msg && *msg) {\n+\t\tstrbuf_addch(sb, '\\t');\n+\t\tstrbuf_addstr(sb, msg);\n+\t}\n+\tstrbuf_addch(sb, '\\n');\n+}\n+\n static int log_ref_write_fd(int fd, const struct object_id *old_oid,\n \t\t\t    const struct object_id *new_oid,\n \t\t\t    const char *committer, const char *msg)\n@@ -1990,15 +2006,7 @@ static int log_ref_write_fd(int fd, const struct object_id *old_oid,\n \tstruct strbuf sb = STRBUF_INIT;\n \tint ret = 0;\n \n-\tif (!committer)\n-\t\tcommitter = git_committer_info(0);\n-\n-\tstrbuf_addf(&sb, \"%s %s %s\", oid_to_hex(old_oid), oid_to_hex(new_oid), committer);\n-\tif (msg && *msg) {\n-\t\tstrbuf_addch(&sb, '\\t');\n-\t\tstrbuf_addstr(&sb, msg);\n-\t}\n-\tstrbuf_addch(&sb, '\\n');\n+\tformat_reflog_entry(&sb, old_oid, new_oid, committer, msg);\n \tif (write_in_full(fd, sb.buf, sb.len) < 0)\n \t\tret = -1;\n \tstrbuf_release(&sb);\n@@ -2397,6 +2405,8 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store,\n \n \twhile (!ret && !strbuf_getwholeline(&sb, logfp, '\\n'))\n \t\tret = show_one_reflog_ent(refs, refname, &sb, fn, cb_data);\n+\tif (ferror(logfp))\n+\t\tret = -1;\n \tfclose(logfp);\n \tstrbuf_release(&sb);\n \treturn ret;\n@@ -2665,12 +2675,120 @@ static enum ref_transaction_error check_old_oid(struct ref_update *update,\n \treturn REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;\n }\n \n+struct staged_reflog {\n+\tstruct tempfile *file;\n+\tstruct ref_update **updates;\n+\tsize_t nr, alloc;\n+};\n+\n struct files_transaction_backend_data {\n \tstruct ref_transaction *packed_transaction;\n \tint packed_refs_locked;\n \tstruct strmap ref_locks;\n+\tstruct strmap reflog_files;\n };\n \n+static int reflog_update_cmp(const void *a, const void *b)\n+{\n+\tconst struct ref_update *one = *(struct ref_update * const *)a;\n+\tconst struct ref_update *two = *(struct ref_update * const *)b;\n+\n+\treturn (one->index > two->index) - (one->index < two->index);\n+}\n+\n+static int prepare_reflog_replacements(struct files_ref_store *refs,\n+\t\t\t\t      struct ref_transaction *transaction,\n+\t\t\t\t      struct strbuf *err)\n+{\n+\tstruct files_transaction_backend_data *data = transaction->backend_data;\n+\tstruct strbuf path = STRBUF_INIT;\n+\tstruct strbuf contents = STRBUF_INIT;\n+\tstruct hashmap_iter iter;\n+\tstruct strmap_entry *entry;\n+\tsize_t i;\n+\tint ret = -1;\n+\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\t\tstruct staged_reflog *log;\n+\n+\t\tif (!(update->flags & REF_REPLACE_REFLOG))\n+\t\t\tcontinue;\n+\t\tCALLOC_ARRAY(log, 1);\n+\t\tstrmap_put(&data->reflog_files, update->refname, log);\n+\t}\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\t\tstruct staged_reflog *log = strmap_get(&data->reflog_files, update->refname);\n+\n+\t\tif (!log || !(update->flags & REF_LOG_ONLY) ||\n+\t\t    !(update->flags & REF_HAVE_NEW))\n+\t\t\tcontinue;\n+\t\tif (!(update->flags & REF_LOG_USE_PROVIDED_OIDS)) {\n+\t\t\tstrbuf_addf(err, \"replacing reflog '%s' requires explicit OIDs\",\n+\t\t\t\t    update->refname);\n+\t\t\tgoto out;\n+\t\t}\n+\t\tALLOC_GROW(log->updates, log->nr + 1, log->alloc);\n+\t\tlog->updates[log->nr++] = update;\n+\t}\n+\tstrmap_for_each_entry(&data->reflog_files, &iter, entry) {\n+\t\tstruct staged_reflog *log = entry->value;\n+\t\tint fd;\n+\n+\t\tif (!log->nr)\n+\t\t\tcontinue;\n+\t\tQSORT(log->updates, log->nr, reflog_update_cmp);\n+\t\t/* Keep the staging file beside the logs, which may be on another device. */\n+\t\tstrbuf_reset(&path);\n+\t\tfiles_reflog_path(refs, &path, is_root_ref(entry->key) ?\n+\t\t\t\t \".tmp-reflog-XXXXXX\" : \"refs/.tmp-reflog-XXXXXX\");\n+\t\tif (safe_create_leading_directories(refs->base.repo, path.buf))\n+\t\t\tgoto write_error;\n+\t\tlog->file = mks_tempfile_m(path.buf, 0666);\n+\t\tif (!log->file)\n+\t\t\tgoto write_error;\n+\t\tfd = get_tempfile_fd(log->file);\n+\t\tfor (i = 0; i < log->nr; i++) {\n+\t\t\tstruct ref_update *update = log->updates[i];\n+\n+\t\t\tformat_reflog_entry(&contents, &update->old_oid, &update->new_oid,\n+\t\t\t\t\t    update->committer_info, update->msg);\n+\t\t\tif (contents.len >= 65536) {\n+\t\t\t\tif (write_in_full(fd, contents.buf, contents.len) < 0)\n+\t\t\t\t\tgoto write_error;\n+\t\t\t\tstrbuf_reset(&contents);\n+\t\t\t}\n+\t\t}\n+\t\tif (write_in_full(fd, contents.buf, contents.len) < 0)\n+\t\t\tgoto write_error;\n+\t\tstrbuf_reset(&contents);\n+\t\tif (adjust_shared_perm(refs->base.repo, get_tempfile_path(log->file)) ||\n+\t\t    close_tempfile_gently(log->file))\n+\t\t\tgoto write_error;\n+\t}\n+\tret = 0;\n+\tgoto out;\n+\n+write_error:\n+\tstrbuf_addf(err, \"cannot write staged reflog: %s\", strerror(errno));\n+out:\n+\tstrbuf_release(&path);\n+\tstrbuf_release(&contents);\n+\treturn ret;\n+}\n+\n+static int install_reflog(const char *path, void *data)\n+{\n+\tstruct staged_reflog *log = data;\n+\tint ret = rename(get_tempfile_path(log->file), path);\n+\n+\t/* Some systems report ENOTDIR when the destination is a directory. */\n+\tif (ret && errno == ENOTDIR)\n+\t\terrno = EISDIR;\n+\treturn ret;\n+}\n+\n /*\n  * Prepare for carrying out update:\n  * - Lock the reference referred to by update.\n@@ -2929,6 +3047,17 @@ static void files_transaction_cleanup(struct files_ref_store *refs,\n \t}\n \n \tif (backend_data) {\n+\t\tstruct hashmap_iter iter;\n+\t\tstruct strmap_entry *entry;\n+\n+\t\tstrmap_for_each_entry(&backend_data->reflog_files, &iter, entry) {\n+\t\t\tstruct staged_reflog *log = entry->value;\n+\n+\t\t\tdelete_tempfile(&log->file);\n+\t\t\tfree(log->updates);\n+\t\t\tfree(log);\n+\t\t}\n+\t\tstrmap_clear(&backend_data->reflog_files, 0);\n \t\tif (backend_data->packed_transaction &&\n \t\t    ref_transaction_abort(backend_data->packed_transaction, &err)) {\n \t\t\terror(\"error aborting transaction: %s\", err.buf);\n@@ -2970,6 +3099,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \n \tCALLOC_ARRAY(backend_data, 1);\n \tstrmap_init(&backend_data->ref_locks);\n+\tstrmap_init(&backend_data->reflog_files);\n \ttransaction->backend_data = backend_data;\n \n \t/*\n@@ -3102,6 +3232,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\t\tif (ret) {\n \t\t\t\tref_transaction_free(packed_transaction);\n \t\t\t\tbackend_data->packed_transaction = NULL;\n+\t\t\t\tgoto cleanup;\n \t\t\t}\n \t\t} else {\n \t\t\t/*\n@@ -3122,6 +3253,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\t}\n \t}\n \n+\tret = prepare_reflog_replacements(refs, transaction, err);\n+\n cleanup:\n \tfree(head_ref);\n \tstring_list_clear(&refnames_to_check, 1);\n@@ -3346,6 +3479,31 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \tbackend_data = transaction->backend_data;\n \tpacked_transaction = backend_data->packed_transaction;\n \n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\t\tstruct staged_reflog *log;\n+\n+\t\tif (!(update->flags & REF_REPLACE_REFLOG))\n+\t\t\tcontinue;\n+\t\tlog = strmap_get(&backend_data->reflog_files, update->refname);\n+\t\tstrbuf_reset(&sb);\n+\t\tfiles_reflog_path(refs, &sb, update->refname);\n+\t\tif (!log->file) {\n+\t\t\tif (unlink(sb.buf) && errno != ENOENT && errno != EISDIR)\n+\t\t\t\tret = -1;\n+\t\t} else if (raceproof_create_file(refs, sb.buf, install_reflog, log)) {\n+\t\t\tret = -1;\n+\t\t} else {\n+\t\t\tdelete_tempfile(&log->file);\n+\t\t}\n+\t\tif (ret) {\n+\t\t\tstrbuf_addf(err, \"cannot replace reflog '%s': %s\",\n+\t\t\t\t    update->refname, strerror(errno));\n+\t\t}\n+\t\tif (ret)\n+\t\t\tgoto cleanup;\n+\t}\n+\n \t/* Perform updates first so live commits remain referenced */\n \tfor (i = 0; i < transaction->nr; i++) {\n \t\tstruct ref_update *update = transaction->updates[i];\n@@ -3354,8 +3512,9 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \t\tif (update->rejection_err)\n \t\t\tcontinue;\n \n-\t\tif (update->flags & REF_NEEDS_COMMIT ||\n-\t\t    update->flags & REF_LOG_ONLY) {\n+\t\tif ((update->flags & REF_NEEDS_COMMIT ||\n+\t\t     update->flags & REF_LOG_ONLY) &&\n+\t\t    !strmap_contains(&backend_data->reflog_files, update->refname)) {\n \t\t\tif (parse_and_write_reflog(refs, update, lock, err)) {\n \t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\t\tgoto cleanup;\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 0b41f5fa4b..19fdf39d1d 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -49,6 +49,9 @@ struct ref_transaction;\n  */\n #define REF_HAVE_PEELED (1 << 15)\n \n+/* Replace a reflog with the explicit log-only updates in the transaction. */\n+#define REF_REPLACE_REFLOG (1 << 16)\n+\n /*\n  * Return the length of time to retry acquiring a loose reference lock\n  * before giving up, in milliseconds:\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 10db03991e..203b111f3d 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1503,9 +1503,11 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data\n \t\t * - `core.logAllRefUpdates` tells us to create the reflog for\n \t\t *   the given ref.\n \t\t */\n-\t\tif ((u->flags & REF_HAVE_NEW) &&\n+\t\tif ((u->flags & REF_REPLACE_REFLOG) ||\n+\t\t    ((u->flags & REF_HAVE_NEW) &&\n+\t\t    !(u->flags & REF_LOG_ONLY) &&\n \t\t    !(u->type & REF_ISSYMREF) &&\n-\t\t    ref_update_has_null_new_value(u)) {\n+\t\t    ref_update_has_null_new_value(u))) {\n \t\t\tstruct reftable_log_record log = {0};\n \t\t\tstruct reftable_iterator it = {0};\n \n@@ -1548,8 +1550,10 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data\n \n \t\t\tif (ret)\n \t\t\t\tgoto done;\n-\t\t} else if (!(u->flags & REF_SKIP_CREATE_REFLOG) &&\n+\t\t}\n+\t\tif (!(u->flags & REF_SKIP_CREATE_REFLOG) &&\n \t\t\t   (u->flags & REF_HAVE_NEW) &&\n+\t\t\t   (!ref_update_has_null_new_value(u) || (u->flags & REF_LOG_ONLY)) &&\n \t\t\t   (u->flags & REF_FORCE_CREATE_REFLOG ||\n \t\t\t    should_write_log(arg->refs, u->refname))) {\n \t\t\tstruct reftable_log_record *log;\ndiff --git a/t/helper/test-ref-store.c b/t/helper/test-ref-store.c\nindex db58f00589..7ce10c28af 100644\n--- a/t/helper/test-ref-store.c\n+++ b/t/helper/test-ref-store.c\n@@ -146,6 +146,45 @@ static int cmd_rename_ref(struct ref_store *refs, const char **argv)\n \treturn refs_rename_ref(refs, oldref, newref, logmsg);\n }\n \n+static int cmd_reflog_transaction(struct ref_store *refs, const char **argv)\n+{\n+\tconst char *refname = notnull(*argv++, \"refname\");\n+\tconst char *mode = notnull(*argv++, \"mode\");\n+\tstruct strbuf err = STRBUF_INIT;\n+\tstruct ref_transaction *transaction = ref_store_transaction_begin(refs, 0, &err);\n+\tint ret = !transaction;\n+\n+\tif (!ret) {\n+\t\tif (!strcmp(mode, \"replace\")) {\n+\t\t\tret = ref_transaction_replace_reflog(transaction, refname, &err);\n+\t\t} else if (!strcmp(mode, \"replace-twice\")) {\n+\t\t\tret = ref_transaction_replace_reflog(transaction, refname, &err) ||\n+\t\t\t\tref_transaction_replace_reflog(transaction, refname, &err);\n+\t\t} else if (strcmp(mode, \"append\")) {\n+\t\t\tdie(\"unknown reflog transaction mode: %s\", mode);\n+\t\t}\n+\t}\n+\twhile (!ret && *argv) {\n+\t\tuint64_t index = strtoumax(notnull(*argv++, \"index\"), NULL, 10);\n+\t\tstruct object_id old_oid, new_oid;\n+\t\tconst char *message;\n+\n+\t\tif (get_oid_hex(notnull(*argv++, \"old-oid\"), &old_oid) ||\n+\t\t    get_oid_hex(notnull(*argv++, \"new-oid\"), &new_oid))\n+\t\t\tdie(\"invalid object ID\");\n+\t\tmessage = notnull(*argv++, \"message\");\n+\t\tret = ref_transaction_update_reflog(transaction, refname,\n+\t\t\t\t&new_oid, &old_oid, NULL, message, index, &err);\n+\t}\n+\tif (!ret)\n+\t\tret = ref_transaction_commit(transaction, &err);\n+\tif (ret)\n+\t\terror(\"%s\", err.buf);\n+\tref_transaction_free(transaction);\n+\tstrbuf_release(&err);\n+\treturn ret;\n+}\n+\n static int each_ref(const struct reference *ref, void *cb_data UNUSED)\n {\n \tprintf(\"%s %s 0x%x\\n\", oid_to_hex(ref->oid), ref->name, ref->flags);\n@@ -308,6 +347,7 @@ static struct command commands[] = {\n \t{ \"create-symref\", cmd_create_symref },\n \t{ \"delete-refs\", cmd_delete_refs },\n \t{ \"rename-ref\", cmd_rename_ref },\n+\t{ \"reflog-transaction\", cmd_reflog_transaction },\n \t{ \"for-each-ref\", cmd_for_each_ref },\n \t{ \"for-each-ref--exclude\", cmd_for_each_ref__exclude },\n \t{ \"resolve-ref\", cmd_resolve_ref },\ndiff --git a/t/meson.build b/t/meson.build\nindex f65eb04684..fb5266cd0d 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -218,6 +218,7 @@ integration_tests = [\n   't1421-reflog-write.sh',\n   't1422-show-ref-exists.sh',\n   't1423-ref-backend.sh',\n+  't1425-reflog-transaction.sh',\n   't1430-bad-ref-name.sh',\n   't1450-fsck.sh',\n   't1451-fsck-buffer.sh',\ndiff --git a/t/t1425-reflog-transaction.sh b/t/t1425-reflog-transaction.sh\nnew file mode 100755\nindex 0000000000..ee5278045e\n--- /dev/null\n+++ b/t/t1425-reflog-transaction.sh\n@@ -0,0 +1,59 @@\n+#!/bin/sh\n+\n+test_description='explicit reflog entries in reference transactions'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\ttest_commit A &&\n+\ttest_commit B &&\n+\tA=$(git rev-parse A) &&\n+\tB=$(git rev-parse B)\n+'\n+\n+test_expect_success 'log-only entry with null new OID does not delete existing history' '\n+\tgit branch source \"$A\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/source >before &&\n+\ttest-tool ref-store main reflog-transaction refs/heads/source append \\\n+\t\t1 \"$A\" \"$ZERO_OID\" deletion &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/source >after &&\n+\tsed \"$ d\" after >history &&\n+\ttest_cmp before history &&\n+\ttest_grep \"$A $ZERO_OID .*deletion\" after &&\n+\ttest_cmp_rev \"$A\" source\n+'\n+\n+test_expect_success 'replace reflog with entries ordered by index' '\n+\ttest-tool ref-store main reflog-transaction refs/heads/source replace \\\n+\t\t2 \"$A\" \"$B\" second \\\n+\t\t1 \"$ZERO_OID\" \"$A\" first &&\n+\tgit reflog show --format=%gs source >actual &&\n+\tprintf \"%s\\n\" second first >expect &&\n+\ttest_cmp expect actual &&\n+\ttest_cmp_rev \"$A\" source\n+'\n+\n+test_expect_success 'prepared hook can reject replacement without losing history' '\n+\ttest_when_finished \"rm -f .git/hooks/reference-transaction\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/source >before &&\n+\twrite_script .git/hooks/reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\ttest_must_fail test-tool ref-store main reflog-transaction refs/heads/source replace \\\n+\t\t1 \"$ZERO_OID\" \"$B\" replacement &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/source >after &&\n+\ttest_cmp before after\n+'\n+\n+test_expect_success 'replacement with no entries removes only the reflog' '\n+\ttest-tool ref-store main reflog-transaction refs/heads/source replace &&\n+\ttest_must_fail git reflog exists refs/heads/source &&\n+\ttest_cmp_rev \"$A\" source\n+'\n+\n+test_expect_success 'duplicate replacement is rejected' '\n+\ttest_must_fail test-tool ref-store main reflog-transaction \\\n+\t\trefs/heads/source replace-twice\n+'\n+\n+test_done\n-- \n2.39.3 (Apple Git-146)\n\n\n"},{"id":"554465","messageId":"150349f9d01e89ed460ed3bd4590139e1e524e67.1791452597.git.maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"cover.1791452597.git.maciej.ciemborowicz@gmail.com","subject":"[PATCH v4 3/4] refs: run copy and rename through ordinary transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-08T09:44:18Z","receivedAt":"2026-10-08T09:44:18Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"Copy and rename currently bypass reference transactions. A hook sees the\nsource deletion with files and sees neither endpoint with reftable.\nQueue the source and destination as ordinary updates instead, with the\ndestination referring to its source update.\n\nRead and lock the source during prepare, then replay its history through\nthe explicit reflog API. Keep this state per update so multiple\nindependent operations and ordinary updates can share a transaction. Do\nnot reject source changes made before locking: preparing is advisory,\nwhile prepared sees the values read under lock and unchanged on-disk\nrefs and reflogs.\n\nFor files D/F and case-only renames, the source prevents locking a loose\ndestination. Queue the destination in packed-refs, protected by the\nsource lock (and an ancestor lock when needed). Suppress hooks for this\nphysical child transaction only. Keep source logs until finish and use\nthe staged replacements so a veto needs no rollback writes. During\nfinish, retain a unique backup of a D/F source log until its destination\nlog is installed, and restore it if installation fails.\n\nPreserve the backends' distinct final reflog records. Forced reftable\ncopies now replace destination history instead of retaining unrelated\ndestination entries. Add regression tests for hooks, full histories,\nHEAD, source races, mixed transactions, lock and installation errors,\nrestoration and cleanup, and a reproducible performance test.\n\nReplaying history costs O(N) time and memory. In a local no-hook\nbenchmark with 10,000 entries, files rename takes about 17 ms versus 5\nms before, and reftable rename about 54 ms versus 44 ms. Short-history\noperations remain within roughly 1 ms. This trades the files\nconstant-time rename for staging that does not expose partial changes to\na prepared hook.\n\nHelped-by: Karthik Nayak <karthik.188@gmail.com>\nHelped-by: Junio C Hamano <gitster@pobox.com>\nHelped-by: Patrick Steinhardt <ps@pks.im>\nSigned-off-by: Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com>\n---\n Documentation/githooks.adoc     |  10 +\n refs.c                          | 163 +++++++++++++--\n refs.h                          |  16 ++\n refs/files-backend.c            | 225 +++++++++++++++++++-\n refs/refs-internal.h            |  13 ++\n refs/reftable-backend.c         |  37 +++-\n t/helper/test-ref-store.c       |  36 ++++\n t/meson.build                   |   1 +\n t/perf/p1424-ref-copy-rename.sh |  48 +++++\n t/t1424-ref-copy-transaction.sh | 352 ++++++++++++++++++++++++++++++++\n 10 files changed, 881 insertions(+), 20 deletions(-)\n create mode 100755 t/perf/p1424-ref-copy-rename.sh\n create mode 100755 t/t1424-ref-copy-transaction.sh\n\ndiff --git a/Documentation/githooks.adoc b/Documentation/githooks.adoc\nindex 145642bf05..245fec3b70 100644\n--- a/Documentation/githooks.adoc\n+++ b/Documentation/githooks.adoc\n@@ -518,6 +518,16 @@ distinguish these cases, you can inspect the current value of\n references are not resolved: `<ref-name>` will reflect the symbolic reference\n itself rather than the object it points to.\n \n+Copying or renaming a reference is reported as one transaction. A rename\n+reports the deletion of the source and the update of the destination;\n+a copy reports only the destination update. Copying the reflog does not\n+produce additional hook input. In the \"preparing\" state, the old values\n+are unspecified (all zeroes), and the destination's new value is the\n+source value read before locking. In subsequent states, these operations\n+report the old values and the source value read under lock. Thus, a\n+\"preparing\" hook may change the source before it is copied or renamed.\n+A \"prepared\" hook still sees the original references and reflogs on disk.\n+\n For symbolic reference updates the `<old_value>` and `<new-value>`\n fields could denote references instead of objects. A reference will be\n denoted with a 'ref:' prefix, like `ref:<ref-target>`.\ndiff --git a/refs.c b/refs.c\nindex c02da9d966..df682e0e26 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -3181,28 +3181,165 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,\n \treturn ret;\n }\n \n+static int transaction_copy_ref(struct ref_transaction *transaction,\n+\t\t\t\tconst char *oldref, const char *newref,\n+\t\t\t\tunsigned int source_flags, const char *logmsg,\n+\t\t\t\tstruct strbuf *err)\n+{\n+\tstruct ref_update *source, *destination;\n+\tstruct object_id oid;\n+\tint type;\n+\n+\tif (transaction->state != REF_TRANSACTION_OPEN)\n+\t\tBUG(\"copy added to a prepared transaction\");\n+\tif (!refs_resolve_ref_unsafe(transaction->ref_store, oldref,\n+\t\t\tRESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, &oid, &type)) {\n+\t\tstrbuf_addf(err, _(\"refname %s not found\"), oldref);\n+\t\treturn -1;\n+\t}\n+\tif (type & REF_ISSYMREF) {\n+\t\tstrbuf_addf(err, _(\"refname %s is a symbolic ref, copying or renaming it is not supported\"), oldref);\n+\t\treturn -1;\n+\t}\n+\tif (!strcmp(oldref, newref))\n+\t\treturn 0;\n+\tif (!transaction_refname_valid(newref, &oid, REF_NO_DEREF, err))\n+\t\treturn -1;\n+\tif (transaction->flags & (REF_TRANSACTION_FLAG_INITIAL | REF_TRANSACTION_ALLOW_FAILURE)) {\n+\t\tstrbuf_addstr(err, _(\"copy and rename require an all-or-nothing, non-initial transaction\"));\n+\t\treturn -1;\n+\t}\n+\n+\t/* This value describes the request to 'preparing'; locks decide the value used. */\n+\tsource = ref_transaction_add_update(transaction, oldref,\n+\t\t\tREF_NO_DEREF | REF_COPY_SOURCE | source_flags,\n+\t\t\t(source_flags & REF_HAVE_NEW) ? null_oid(transaction->ref_store->repo->hash_algo) : NULL,\n+\t\t\tNULL, NULL, NULL, NULL, NULL, logmsg);\n+\tdestination = ref_transaction_add_update(transaction, newref,\n+\t\t\tREF_NO_DEREF | REF_HAVE_NEW,\n+\t\t\t&oid, NULL, NULL, NULL, NULL, NULL, logmsg);\n+\tdestination->copy_from = source;\n+\treturn ref_transaction_replace_reflog(transaction, newref, err);\n+}\n+\n+int ref_transaction_copy(struct ref_transaction *transaction,\n+\t\t\t const char *oldref, const char *newref,\n+\t\t\t const char *logmsg, struct strbuf *err)\n+{\n+\treturn transaction_copy_ref(transaction, oldref, newref,\n+\t\t\t\t    REF_LOG_ONLY | REF_SKIP_CREATE_REFLOG, logmsg, err);\n+}\n+\n+int ref_transaction_rename(struct ref_transaction *transaction,\n+\t\t\t   const char *oldref, const char *newref,\n+\t\t\t   const char *logmsg, struct strbuf *err)\n+{\n+\treturn transaction_copy_ref(transaction, oldref, newref,\n+\t\t\t\t    REF_HAVE_NEW, logmsg, err);\n+}\n+\n+int ref_update_record_copy_source(struct ref_update *update,\n+\t\t\t\t  const struct object_id *oid,\n+\t\t\t\t  struct strbuf *err)\n+{\n+\tif ((update->type & REF_ISSYMREF) || is_null_oid(oid)) {\n+\t\tstrbuf_addf(err, _(\"'%s' is not an existing direct reference\"), update->refname);\n+\t\treturn -1;\n+\t}\n+\toidcpy(&update->old_oid, oid);\n+\tupdate->flags |= REF_HAVE_OLD;\n+\treturn 0;\n+}\n+\n+struct copy_reflog_data {\n+\tstruct ref_transaction *transaction;\n+\tconst char *refname;\n+\tstruct strbuf *err;\n+};\n+\n+static int copy_reflog_entry(const char *refname UNUSED,\n+\t\t\t     struct object_id *old_oid, struct object_id *new_oid,\n+\t\t\t     const char *committer, timestamp_t timestamp, int tz,\n+\t\t\t     const char *message, void *cb_data)\n+{\n+\tstruct copy_reflog_data *data = cb_data;\n+\tstruct strbuf ident = STRBUF_INIT;\n+\tint ret;\n+\n+\tstrbuf_addf(&ident, \"%s %\" PRItime \" %+05d\", committer, timestamp, tz);\n+\tret = ref_transaction_update_reflog(data->transaction, data->refname,\n+\t\t\tnew_oid, old_oid, ident.buf, message,\n+\t\t\tdata->transaction->max_index + 1, data->err);\n+\tstrbuf_release(&ident);\n+\treturn ret;\n+}\n+\n+int ref_transaction_prepare_copy(struct ref_transaction *transaction,\n+\t\t\t\t struct ref_update *update,\n+\t\t\t\t struct strbuf *err)\n+{\n+\tstruct ref_update *source = update->copy_from;\n+\tstruct copy_reflog_data data = { transaction, update->refname, err };\n+\tstruct object *object;\n+\tint ret;\n+\n+\toidcpy(&update->new_oid, &source->old_oid);\n+\tobject = parse_object(transaction->ref_store->repo, &update->new_oid);\n+\tif (!object || (is_branch(update->refname) && object->type != OBJ_COMMIT)) {\n+\t\tstrbuf_addf(err, _(\"cannot copy object %s to '%s'\"),\n+\t\t\t    oid_to_hex(&update->new_oid), update->refname);\n+\t\treturn -1;\n+\t}\n+\tif (object->type == OBJ_TAG &&\n+\t    !peel_object(transaction->ref_store->repo, &update->new_oid,\n+\t\t\t &update->peeled, PEEL_OBJECT_VERIFY_TAGGED_OBJECT_TYPE))\n+\t\tupdate->flags |= REF_HAVE_PEELED;\n+\n+\tif (!refs_reflog_exists(transaction->ref_store, source->refname))\n+\t\treturn 0;\n+\tret = refs_for_each_reflog_ent(transaction->ref_store, source->refname,\n+\t\t\t\t       copy_reflog_entry, &data);\n+\tif (ret && !err->len)\n+\t\tstrbuf_addf(err, _(\"cannot read reflog for '%s'\"), source->refname);\n+\treturn ret;\n+}\n+\n int refs_rename_ref(struct ref_store *refs, const char *oldref,\n \t\t    const char *newref, const char *logmsg)\n {\n-\tchar *msg;\n-\tint retval;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tstruct ref_transaction *transaction = ref_store_transaction_begin(refs, 0, &err);\n+\tint ret = -1;\n \n-\tmsg = normalize_reflog_message(logmsg);\n-\tretval = refs->be->rename_ref(refs, oldref, newref, msg);\n-\tfree(msg);\n-\treturn retval;\n+\tif (!transaction ||\n+\t    ref_transaction_rename(transaction, oldref, newref, logmsg, &err))\n+\t\tgoto out;\n+\tret = transaction->nr ? ref_transaction_commit(transaction, &err) : 0;\n+out:\n+\tif (ret)\n+\t\terror(\"%s\", err.buf);\n+\tref_transaction_free(transaction);\n+\tstrbuf_release(&err);\n+\treturn ret;\n }\n \n int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,\n-\t\t    const char *newref, const char *logmsg)\n+\t\t\t   const char *newref, const char *logmsg)\n {\n-\tchar *msg;\n-\tint retval;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tstruct ref_transaction *transaction = ref_store_transaction_begin(refs, 0, &err);\n+\tint ret = -1;\n \n-\tmsg = normalize_reflog_message(logmsg);\n-\tretval = refs->be->copy_ref(refs, oldref, newref, msg);\n-\tfree(msg);\n-\treturn retval;\n+\tif (!transaction ||\n+\t    ref_transaction_copy(transaction, oldref, newref, logmsg, &err))\n+\t\tgoto out;\n+\tret = transaction->nr ? ref_transaction_commit(transaction, &err) : 0;\n+out:\n+\tif (ret)\n+\t\terror(\"%s\", err.buf);\n+\tref_transaction_free(transaction);\n+\tstrbuf_release(&err);\n+\treturn ret;\n }\n \n const char *ref_update_original_update_refname(struct ref_update *update)\ndiff --git a/refs.h b/refs.h\nindex b0b2b0e4fd..eba5025351 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -952,6 +952,22 @@ int ref_transaction_replace_reflog(struct ref_transaction *transaction,\n \t\t\t\t    const char *refname,\n \t\t\t\t    struct strbuf *err);\n \n+/*\n+ * Queue a copy or rename of a direct reference and its reflog. The source is\n+ * read again under lock during prepare; changes made by the preparing hook\n+ * are included. An existing destination is overwritten without dereferencing\n+ * it. Several independent copies, renames and ordinary updates may be queued\n+ * together. Each destination (and each source being renamed) must be unique.\n+ * Initial transactions and transactions allowing individual failures are not\n+ * supported. On failure, discard the transaction.\n+ */\n+int ref_transaction_copy(struct ref_transaction *transaction,\n+\t\t\t const char *oldref, const char *newref,\n+\t\t\t const char *logmsg, struct strbuf *err);\n+int ref_transaction_rename(struct ref_transaction *transaction,\n+\t\t\t   const char *oldref, const char *newref,\n+\t\t\t   const char *logmsg, struct strbuf *err);\n+\n /*\n  * Add a reference creation to transaction. new_oid is the value that\n  * the reference should have after the update; it must not be\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 36ecd21ed7..ffdc112f9a 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -74,6 +74,12 @@ extern int ignore_case;\n  */\n #define REF_LOG_VIA_SPLIT (1 << 14)\n \n+/* A D/F or case-only rename cannot lock a loose destination beside its source. */\n+#define REF_NEEDS_PACK (1 << 18)\n+\n+/* The source reflog was removed to make room for its rename destination. */\n+#define REF_RENAMED_LOG (1 << 19)\n+\n struct ref_lock {\n \tchar *ref_name;\n \tstruct lock_file lk;\n@@ -788,6 +794,37 @@ static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,\n \tlock->count = 1;\n \tfiles_ref_path(refs, &ref_file, refname);\n \n+\tif (update->copy_from && (update->copy_from->flags & REF_HAVE_NEW)) {\n+\t\tconst char *source = update->copy_from->refname;\n+\t\tsize_t source_len = strlen(source), dest_len = strlen(refname);\n+\t\tint source_is_parent = starts_with(refname, source) && refname[source_len] == '/';\n+\t\tint dest_is_parent = starts_with(source, refname) && source[dest_len] == '/';\n+\t\tint same_path = repo_ignore_case(refs->base.repo) && !strcasecmp(source, refname);\n+\n+\t\tif (source_is_parent || dest_is_parent || same_path) {\n+\t\t\tstruct string_list skip = STRING_LIST_INIT_NODUP;\n+\n+\t\t\tstring_list_insert(&skip, source);\n+\t\t\tret = refs_verify_refname_available(&refs->base, refname,\n+\t\t\t\t\t\t\t   extras, &skip, 0, err);\n+\t\t\tstring_list_clear(&skip, 0);\n+\t\t\tif (ret)\n+\t\t\t\tgoto error_return;\n+\t\t\t/* The existing source and its lock protect a child or case-only destination. */\n+\t\t\tif (dest_is_parent && repo_hold_lock_file_for_update_timeout(\n+\t\t\t\t\trefs->base.repo, &lock->lk, ref_file.buf, LOCK_NO_DEREF,\n+\t\t\t\t\tget_files_ref_lock_timeout_ms(refs->base.repo)) < 0) {\n+\t\t\t\tunable_to_lock_message(ref_file.buf, errno, err);\n+\t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n+\t\t\t\tgoto error_return;\n+\t\t\t}\n+\t\t\toidclr(&lock->old_oid, refs->base.repo->hash_algo);\n+\t\t\tupdate->flags |= REF_NEEDS_PACK;\n+\t\t\tret = 0;\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n retry:\n \tswitch (safe_create_leading_directories(refs->base.repo, ref_file.buf)) {\n \tcase SCLD_OK:\n@@ -2538,7 +2575,8 @@ static enum ref_transaction_error split_head_update(struct ref_update *update,\n \n \tnew_update = ref_transaction_add_update(\n \t\t\ttransaction, \"HEAD\",\n-\t\t\tupdate->flags | REF_LOG_ONLY | REF_NO_DEREF | REF_LOG_VIA_SPLIT,\n+\t\t\t(update->flags & ~REF_COPY_SOURCE) |\n+\t\t\tREF_LOG_ONLY | REF_NO_DEREF | REF_LOG_VIA_SPLIT,\n \t\t\t&update->new_oid, &update->old_oid, &update->peeled,\n \t\t\tNULL, NULL, update->committer_info, update->msg);\n \tnew_update->parent_update = update;\n@@ -2677,6 +2715,7 @@ static enum ref_transaction_error check_old_oid(struct ref_update *update,\n \n struct staged_reflog {\n \tstruct tempfile *file;\n+\tstruct tempfile *source_log;\n \tstruct ref_update **updates;\n \tsize_t nr, alloc;\n };\n@@ -2789,6 +2828,155 @@ static int install_reflog(const char *path, void *data)\n \treturn ret;\n }\n \n+static int move_reflog(const char *path, void *data)\n+{\n+\tstruct tempfile *tempfile = data;\n+\tint ret = rename(get_tempfile_path(tempfile), path);\n+\n+\tif (ret && errno == ENOTDIR)\n+\t\terrno = EISDIR;\n+\treturn ret;\n+}\n+\n+static int stage_source_reflog(struct files_ref_store *refs,\n+\t\t\t       struct ref_update *update,\n+\t\t\t       struct staged_reflog *log,\n+\t\t\t       struct strbuf *err)\n+{\n+\tstruct strbuf source = STRBUF_INIT;\n+\tstruct strbuf path = STRBUF_INIT;\n+\tint ret = -1;\n+\n+\tfiles_reflog_path(refs, &source, update->copy_from->refname);\n+\tif (!refs_reflog_exists(&refs->base, update->copy_from->refname)) {\n+\t\tret = 0;\n+\t\tgoto out;\n+\t}\n+\tfiles_reflog_path(refs, &path, \"refs/.tmp-reflog-source-XXXXXX\");\n+\tlog->source_log = mks_tempfile_m(path.buf, 0666);\n+\tif (!log->source_log || close_tempfile_gently(log->source_log) ||\n+\t    rename(source.buf, get_tempfile_path(log->source_log))) {\n+\t\tstrbuf_addf(err, \"cannot stage source reflog '%s': %s\",\n+\t\t\t    update->copy_from->refname, strerror(errno));\n+\t\tgoto out;\n+\t}\n+\ttry_remove_empty_parents(refs, update->copy_from->refname,\n+\t\t\t\t REMOVE_EMPTY_PARENTS_REFLOG);\n+\tupdate->copy_from->flags |= REF_RENAMED_LOG;\n+\tret = 0;\n+\n+out:\n+\tif (ret)\n+\t\tdelete_tempfile(&log->source_log);\n+\tstrbuf_release(&source);\n+\tstrbuf_release(&path);\n+\treturn ret;\n+}\n+\n+static int restore_source_reflog(struct files_ref_store *refs,\n+\t\t\t\t struct ref_update *update,\n+\t\t\t\t struct staged_reflog *log,\n+\t\t\t\t struct strbuf *err)\n+{\n+\tstruct strbuf source = STRBUF_INIT;\n+\tint ret;\n+\n+\tif (!log->source_log)\n+\t\treturn 0;\n+\tfiles_reflog_path(refs, &source, update->copy_from->refname);\n+\tret = raceproof_create_file(refs, source.buf, move_reflog,\n+\t\t\t\t    log->source_log);\n+\tif (ret) {\n+\t\tstruct strbuf recovery = STRBUF_INIT;\n+\n+\t\tstrbuf_addf(err, \"; cannot restore source reflog '%s': %s\",\n+\t\t\t    update->copy_from->refname, strerror(errno));\n+\t\tstrbuf_addf(&recovery, \"%s.recovery\",\n+\t\t\t    get_tempfile_path(log->source_log));\n+\t\tif (rename_tempfile(&log->source_log, recovery.buf))\n+\t\t\tstrbuf_addf(err, \"; cannot preserve its backup: %s\",\n+\t\t\t\t    strerror(errno));\n+\t\telse\n+\t\t\tstrbuf_addf(err, \"; backup saved as '%s'\", recovery.buf);\n+\t\tstrbuf_release(&recovery);\n+\t} else {\n+\t\tdelete_tempfile(&log->source_log);\n+\t}\n+\tstrbuf_release(&source);\n+\treturn ret;\n+}\n+\n+static int check_reflog_symlink(struct files_ref_store *refs,\n+\t\t\t\tconst char *refname, struct strbuf *err)\n+{\n+\tstruct strbuf path = STRBUF_INIT;\n+\tstruct stat st;\n+\tint ret = 0;\n+\n+\tfiles_reflog_path(refs, &path, refname);\n+\tif (!lstat(path.buf, &st)) {\n+\t\tif (S_ISLNK(st.st_mode)) {\n+\t\t\tstrbuf_addf(err, \"reflog for %s is a symlink\", refname);\n+\t\t\tret = -1;\n+\t\t}\n+\t} else if (errno != ENOENT && errno != ENOTDIR) {\n+\t\tstrbuf_addf(err, \"cannot stat reflog for %s: %s\", refname, strerror(errno));\n+\t\tret = -1;\n+\t}\n+\tstrbuf_release(&path);\n+\treturn ret;\n+}\n+\n+static struct ref_update *find_update_with_flag(struct ref_transaction *transaction,\n+\t\t\t\t\t\tconst char *refname,\n+\t\t\t\t\t\tunsigned int flag)\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 & flag) && !strcmp(update->refname, refname))\n+\t\t\treturn update;\n+\t}\n+\treturn NULL;\n+}\n+\n+static int prepare_copy_reflog(struct files_ref_store *refs,\n+\t\t\t       struct ref_transaction *transaction,\n+\t\t\t       struct ref_update *update, struct strbuf *err)\n+{\n+\tenum log_refs_config config = files_ref_store_write_options(refs)->log_all_ref_updates;\n+\tint source_has_log;\n+\tint write_log;\n+\n+\tif (check_reflog_symlink(refs, update->copy_from->refname, err) ||\n+\t    check_reflog_symlink(refs, update->refname, err))\n+\t\treturn -1;\n+\tsource_has_log = refs_reflog_exists(&refs->base, update->copy_from->refname);\n+\t/* A copy without source history only appends to an existing destination log. */\n+\tif (!source_has_log && !(update->copy_from->flags & REF_HAVE_NEW)) {\n+\t\tstruct ref_update *replacement = find_update_with_flag(\n+\t\t\ttransaction, update->refname, REF_REPLACE_REFLOG);\n+\n+\t\tif (!replacement)\n+\t\t\tBUG(\"copy destination without a reflog replacement\");\n+\t\treplacement->flags &= ~REF_REPLACE_REFLOG;\n+\t}\n+\tif (config == LOG_REFS_UNSET)\n+\t\tconfig = is_bare_repository(refs->base.repo) ? LOG_REFS_NONE : LOG_REFS_NORMAL;\n+\twrite_log = source_has_log || should_autocreate_reflog(config, update->refname) ||\n+\t\t(!(update->copy_from->flags & REF_HAVE_NEW) &&\n+\t\t refs_reflog_exists(&refs->base, update->refname));\n+\tif (ref_transaction_prepare_copy(transaction, update, err))\n+\t\treturn -1;\n+\tif (!write_log)\n+\t\treturn 0;\n+\treturn ref_transaction_update_reflog(transaction, update->refname,\n+\t\t\t&update->new_oid, &update->new_oid, NULL, update->msg,\n+\t\t\ttransaction->max_index + 1, err);\n+}\n+\n /*\n  * Prepare for carrying out update:\n  * - Lock the reference referred to by update.\n@@ -2819,6 +3007,8 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re\n \tfiles_assert_main_repository(refs, \"lock_ref_for_update\");\n \n \tbackend_data = transaction->backend_data;\n+\tif (update->copy_from && prepare_copy_reflog(refs, transaction, update, err))\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \n \tif ((update->flags & REF_HAVE_NEW) && ref_update_has_null_new_value(update))\n \t\tupdate->flags |= REF_DELETING;\n@@ -2828,6 +3018,8 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re\n \t\tif (ret)\n \t\t\tgoto out;\n \t}\n+\tif (update->copy_from)\n+\t\tupdate->flags |= REF_SKIP_CREATE_REFLOG;\n \n \tlock = strmap_get(&backend_data->ref_locks, update->refname);\n \tif (lock) {\n@@ -2849,6 +3041,17 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re\n \t}\n \n \tupdate->backend_data = lock;\n+\tif (update->copy_from) {\n+\t\toidcpy(&update->old_oid, &lock->old_oid);\n+\t\tif (update->type & REF_ISSYMREF)\n+\t\t\tupdate->old_target = xstrdup(referent.buf);\n+\t\tupdate->flags |= REF_HAVE_OLD;\n+\t}\n+\tif ((update->flags & REF_COPY_SOURCE) &&\n+\t    ref_update_record_copy_source(update, &lock->old_oid, err)) {\n+\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n+\t\tgoto out;\n+\t}\n \n \tif (update->flags & REF_LOG_VIA_SPLIT) {\n \t\tstruct ref_lock *parent_lock;\n@@ -2974,7 +3177,7 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re\n \t\tupdate->flags |= REF_NEEDS_COMMIT;\n \t} else if ((update->flags & REF_HAVE_NEW) &&\n \t\t   !(update->flags & REF_DELETING) &&\n-\t\t   !(update->flags & REF_LOG_ONLY)) {\n+\t\t   !(update->flags & (REF_LOG_ONLY | REF_NEEDS_PACK))) {\n \t\tif (!(update->type & REF_ISSYMREF) &&\n \t\t    oideq(&lock->old_oid, &update->new_oid)) {\n \t\t\t/*\n@@ -3003,7 +3206,7 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re\n \t\t\t}\n \t\t}\n \t}\n-\tif (!(update->flags & REF_NEEDS_COMMIT)) {\n+\tif (!(update->flags & (REF_NEEDS_COMMIT | REF_NEEDS_PACK))) {\n \t\t/*\n \t\t * We didn't call write_ref_to_lockfile(), so\n \t\t * the lockfile is still open. Close it to\n@@ -3054,6 +3257,7 @@ static void files_transaction_cleanup(struct files_ref_store *refs,\n \t\t\tstruct staged_reflog *log = entry->value;\n \n \t\t\tdelete_tempfile(&log->file);\n+\t\t\tdelete_tempfile(&log->source_log);\n \t\t\tfree(log->updates);\n \t\t\tfree(log);\n \t\t}\n@@ -3162,7 +3366,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\t\tgoto cleanup;\n \t\t}\n \n-\t\tif (update->flags & REF_DELETING &&\n+\t\tif (update->flags & (REF_DELETING | REF_NEEDS_PACK) &&\n \t\t    !(update->flags & REF_LOG_ONLY) &&\n \t\t    !(update->flags & REF_IS_PRUNING) &&\n \t\t    !is_root_ref(update->refname)) {\n@@ -3189,6 +3393,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n \t\t\t\t\tREF_HAVE_NEW | REF_NO_DEREF,\n \t\t\t\t\t&update->new_oid, NULL, NULL,\n \t\t\t\t\tNULL, NULL, NULL, NULL);\n+\t\t\tif (update->copy_from || (update->flags & REF_COPY_SOURCE))\n+\t\t\t\tpacked_transaction->flags |= REF_TRANSACTION_FLAG_INTERNAL;\n \t\t}\n \t}\n \n@@ -3481,11 +3687,18 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \n \tfor (i = 0; i < transaction->nr; i++) {\n \t\tstruct ref_update *update = transaction->updates[i];\n+\t\tstruct ref_update *operation;\n \t\tstruct staged_reflog *log;\n \n \t\tif (!(update->flags & REF_REPLACE_REFLOG))\n \t\t\tcontinue;\n \t\tlog = strmap_get(&backend_data->reflog_files, update->refname);\n+\t\toperation = find_update_with_flag(transaction, update->refname,\n+\t\t\t\t\t\t  REF_NEEDS_PACK);\n+\t\tif (operation && stage_source_reflog(refs, operation, log, err)) {\n+\t\t\tret = -1;\n+\t\t\tgoto cleanup;\n+\t\t}\n \t\tstrbuf_reset(&sb);\n \t\tfiles_reflog_path(refs, &sb, update->refname);\n \t\tif (!log->file) {\n@@ -3499,6 +3712,8 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \t\tif (ret) {\n \t\t\tstrbuf_addf(err, \"cannot replace reflog '%s': %s\",\n \t\t\t\t    update->refname, strerror(errno));\n+\t\t\tif (operation)\n+\t\t\t\trestore_source_reflog(refs, operation, log, err);\n \t\t}\n \t\tif (ret)\n \t\t\tgoto cleanup;\n@@ -3563,7 +3778,7 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \n \t\tif (update->flags & REF_DELETING &&\n \t\t    !(update->flags & REF_LOG_ONLY) &&\n-\t\t    !(update->flags & REF_IS_PRUNING)) {\n+\t\t    !(update->flags & (REF_IS_PRUNING | REF_RENAMED_LOG))) {\n \t\t\tstrbuf_reset(&sb);\n \t\t\tfiles_reflog_path(refs, &sb, update->refname);\n \t\t\tif (!unlink_or_warn(sb.buf))\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 19fdf39d1d..2746ce5b49 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -52,6 +52,9 @@ struct ref_transaction;\n /* Replace a reflog with the explicit log-only updates in the transaction. */\n #define REF_REPLACE_REFLOG (1 << 16)\n \n+/* A source whose value and reflog are consumed by a later update. */\n+#define REF_COPY_SOURCE (1 << 17)\n+\n /*\n  * Return the length of time to retry acquiring a loose reference lock\n  * before giving up, in milliseconds:\n@@ -161,6 +164,9 @@ struct ref_update {\n \t */\n \tstruct ref_update *parent_update;\n \n+\t/* The source is queued first, so backends lock it before the destination. */\n+\tstruct ref_update *copy_from;\n+\n \tconst char refname[FLEX_ARRAY];\n };\n \n@@ -193,6 +199,13 @@ struct ref_update *ref_transaction_add_update(\n \t\tconst char *committer_info,\n \t\tconst char *msg);\n \n+int ref_update_record_copy_source(struct ref_update *update,\n+\t\t\t\t  const struct object_id *oid,\n+\t\t\t\t  struct strbuf *err);\n+int ref_transaction_prepare_copy(struct ref_transaction *transaction,\n+\t\t\t\t struct ref_update *update,\n+\t\t\t\t struct strbuf *err);\n+\n /*\n  * Transaction states.\n  *\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 203b111f3d..72c1596c11 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1076,6 +1076,22 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor\n \tstruct object_id current_oid = {0};\n \tconst char *rewritten_ref;\n \n+\tif (u->copy_from) {\n+\t\tconst struct object_id *zero = null_oid(refs->base.repo->hash_algo);\n+\n+\t\tif (ref_transaction_prepare_copy(transaction, u, err))\n+\t\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n+\t\tif ((u->copy_from->flags & REF_HAVE_NEW) &&\n+\t\t    ref_transaction_update_reflog(transaction, u->refname,\n+\t\t\t\tzero, &u->new_oid, NULL, u->msg,\n+\t\t\t\ttransaction->max_index + 1, err))\n+\t\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n+\t\tif (ref_transaction_update_reflog(transaction, u->refname,\n+\t\t\t\t&u->new_oid, zero, NULL, u->msg,\n+\t\t\t\ttransaction->max_index + 1, err))\n+\t\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n+\t}\n+\n \t/*\n \t * There is no need to reload the respective backends here as\n \t * we have already reloaded them when preparing the transaction\n@@ -1126,15 +1142,27 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor\n \n \t\tref_transaction_add_update(\n \t\t\ttransaction, \"HEAD\",\n-\t\t\tu->flags | REF_LOG_ONLY | REF_NO_DEREF,\n+\t\t\t(u->flags & ~REF_COPY_SOURCE) |\n+\t\t\tREF_LOG_ONLY | REF_NO_DEREF,\n \t\t\t&u->new_oid, &u->old_oid, &u->peeled, NULL, NULL,\n \t\t\tNULL, u->msg);\n \t}\n+\tif (u->copy_from)\n+\t\tu->flags |= REF_SKIP_CREATE_REFLOG;\n \n \tret = reftable_backend_read_ref(be, rewritten_ref,\n \t\t\t\t\t&current_oid, referent, &u->type);\n \tif (ret < 0)\n \t\treturn REF_TRANSACTION_ERROR_GENERIC;\n+\tif (u->copy_from) {\n+\t\toidcpy(&u->old_oid, &current_oid);\n+\t\tif (u->type & REF_ISSYMREF)\n+\t\t\tu->old_target = xstrdup(referent->buf);\n+\t\tu->flags |= REF_HAVE_OLD;\n+\t}\n+\tif ((u->flags & REF_COPY_SOURCE) &&\n+\t    ref_update_record_copy_source(u, &current_oid, err))\n+\t\treturn REF_TRANSACTION_ERROR_GENERIC;\n \tif (ret > 0 && !ref_update_expects_existing_old_ref(u)) {\n \t\tstruct string_list_item *item;\n \t\t/*\n@@ -1319,6 +1347,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\treftable_be_downcast(ref_store, REF_STORE_WRITE|REF_STORE_MAIN, \"ref_transaction_prepare\");\n \tstruct strbuf referent = STRBUF_INIT, head_referent = STRBUF_INIT;\n \tstruct string_list refnames_to_check = STRING_LIST_INIT_NODUP;\n+\tstruct string_list renamed_sources = STRING_LIST_INIT_NODUP;\n \tstruct reftable_transaction_data *tx_data = NULL;\n \tstruct reftable_backend *be;\n \tstruct object_id head_oid;\n@@ -1338,6 +1367,9 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t * that will be modified during the transaction.\n \t */\n \tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\t\tif (update->copy_from && (update->copy_from->flags & REF_HAVE_NEW))\n+\t\t\tstring_list_insert(&renamed_sources, update->copy_from->refname);\n \t\tret = prepare_transaction_update(NULL, refs, tx_data,\n \t\t\t\t\t\t transaction->updates[i], err);\n \t\tif (ret)\n@@ -1390,7 +1422,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t}\n \n \tret = refs_verify_refnames_available(ref_store, &refnames_to_check,\n-\t\t\t\t\t     &transaction->refnames, NULL,\n+\t\t\t\t\t     &transaction->refnames, &renamed_sources,\n \t\t\t\t\t     transaction,\n \t\t\t\t\t     transaction->flags & REF_TRANSACTION_FLAG_INITIAL,\n \t\t\t\t\t     err);\n@@ -1411,6 +1443,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \tstrbuf_release(&referent);\n \tstrbuf_release(&head_referent);\n \tstring_list_clear(&refnames_to_check, 1);\n+\tstring_list_clear(&renamed_sources, 0);\n \n \treturn ret;\n }\ndiff --git a/t/helper/test-ref-store.c b/t/helper/test-ref-store.c\nindex 7ce10c28af..ec7728334c 100644\n--- a/t/helper/test-ref-store.c\n+++ b/t/helper/test-ref-store.c\n@@ -185,6 +185,41 @@ static int cmd_reflog_transaction(struct ref_store *refs, const char **argv)\n \treturn ret;\n }\n \n+static int cmd_copy_transaction(struct ref_store *refs, const char **argv)\n+{\n+\tstruct strbuf err = STRBUF_INIT;\n+\tstruct ref_transaction *transaction = ref_store_transaction_begin(refs, 0, &err);\n+\tint ret = !transaction;\n+\n+\twhile (!ret && *argv) {\n+\t\tconst char *operation = *argv++;\n+\t\tconst char *source = notnull(*argv++, \"source\");\n+\t\tconst char *destination = notnull(*argv++, \"destination\");\n+\n+\t\tif (!strcmp(operation, \"rename\"))\n+\t\t\tret = ref_transaction_rename(transaction, source, destination, \"rename\", &err);\n+\t\telse if (!strcmp(operation, \"copy\"))\n+\t\t\tret = ref_transaction_copy(transaction, source, destination, \"copy\", &err);\n+\t\telse if (!strcmp(operation, \"update\")) {\n+\t\t\tstruct object_id oid;\n+\n+\t\t\tif (get_oid_hex(destination, &oid))\n+\t\t\t\tdie(\"invalid object ID: %s\", destination);\n+\t\t\tret = ref_transaction_update(transaction, source, &oid,\n+\t\t\t\t\t\t     NULL, NULL, NULL, 0, \"update\", &err);\n+\t\t} else {\n+\t\t\tdie(\"unknown operation: %s\", operation);\n+\t\t}\n+\t}\n+\tif (!ret)\n+\t\tret = ref_transaction_commit(transaction, &err);\n+\tif (ret)\n+\t\terror(\"%s\", err.buf);\n+\tref_transaction_free(transaction);\n+\tstrbuf_release(&err);\n+\treturn ret;\n+}\n+\n static int each_ref(const struct reference *ref, void *cb_data UNUSED)\n {\n \tprintf(\"%s %s 0x%x\\n\", oid_to_hex(ref->oid), ref->name, ref->flags);\n@@ -348,6 +383,7 @@ static struct command commands[] = {\n \t{ \"delete-refs\", cmd_delete_refs },\n \t{ \"rename-ref\", cmd_rename_ref },\n \t{ \"reflog-transaction\", cmd_reflog_transaction },\n+\t{ \"copy-transaction\", cmd_copy_transaction },\n \t{ \"for-each-ref\", cmd_for_each_ref },\n \t{ \"for-each-ref--exclude\", cmd_for_each_ref__exclude },\n \t{ \"resolve-ref\", cmd_resolve_ref },\ndiff --git a/t/meson.build b/t/meson.build\nindex fb5266cd0d..36d47c92c4 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -218,6 +218,7 @@ integration_tests = [\n   't1421-reflog-write.sh',\n   't1422-show-ref-exists.sh',\n   't1423-ref-backend.sh',\n+  't1424-ref-copy-transaction.sh',\n   't1425-reflog-transaction.sh',\n   't1430-bad-ref-name.sh',\n   't1450-fsck.sh',\ndiff --git a/t/perf/p1424-ref-copy-rename.sh b/t/perf/p1424-ref-copy-rename.sh\nnew file mode 100755\nindex 0000000000..a901129b00\n--- /dev/null\n+++ b/t/perf/p1424-ref-copy-rename.sh\n@@ -0,0 +1,48 @@\n+#!/bin/sh\n+\n+test_description='Copy and rename references with short and long reflogs'\n+\n+. ./perf-lib.sh\n+\n+test_perf_fresh_repo\n+\n+for entries in 10 10000\n+do\n+\texport entries\n+\ttest_expect_success \"setup $entries reflog entries\" '\n+\t\tgit init --ref-format=files \"repo-$entries\" &&\n+\t\t(\n+\t\t\tcd \"repo-$entries\" &&\n+\t\t\ttest_commit A &&\n+\t\t\tgit branch source &&\n+\t\t\toid=$(git rev-parse HEAD) &&\n+\t\t\tawk -v oid=\"$oid\" -v count=\"$entries\" '\\''\n+\t\t\t\tBEGIN {\n+\t\t\t\t\tfor (i = 0; i < count; i++)\n+\t\t\t\t\t\tprintf \"%s %s A <a@example.com> %d +0000\\tentry %d\\n\", oid, oid, 1700000000 + i, i\n+\t\t\t\t}\n+\t\t\t'\\'' >.git/logs/refs/heads/source &&\n+\t\t\tif test \"$GIT_TEST_DEFAULT_REF_FORMAT\" = reftable\n+\t\t\tthen\n+\t\t\t\tgit refs migrate --ref-format=reftable\n+\t\t\tfi\n+\t\t)\n+\t'\n+\n+\ttest_perf \"copy $entries reflog entries (10 operations)\" '\n+\t\tfor i in $(test_seq 10)\n+\t\tdo\n+\t\t\tgit -C \"repo-$entries\" branch -C source destination || return 1\n+\t\tdone\n+\t'\n+\n+\ttest_perf \"rename $entries reflog entries (10 operations)\" '\n+\t\tfor i in $(test_seq 5)\n+\t\tdo\n+\t\t\tgit -C \"repo-$entries\" branch -m source renamed &&\n+\t\t\tgit -C \"repo-$entries\" branch -m renamed source || return 1\n+\t\tdone\n+\t'\n+done\n+\n+test_done\ndiff --git a/t/t1424-ref-copy-transaction.sh b/t/t1424-ref-copy-transaction.sh\nnew file mode 100755\nindex 0000000000..4c7592795f\n--- /dev/null\n+++ b/t/t1424-ref-copy-transaction.sh\n@@ -0,0 +1,352 @@\n+#!/bin/sh\n+\n+test_description='reference transactions for copy and rename'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+snapshot () {\n+\tgit for-each-ref --format=\"%(refname) %(objectname) %(symref)\" >\"$1.refs\" &&\n+\tfor ref in HEAD refs/heads/source refs/heads/destination\n+\tdo\n+\t\tif git reflog exists \"$ref\"\n+\t\tthen\n+\t\t\ttest-tool ref-store main for-each-reflog-ent \"$ref\"\n+\t\telse\n+\t\t\techo missing\n+\t\tfi >\"$1.$(basename \"$ref\").log\" || return 1\n+\tdone\n+}\n+\n+compare_snapshot () {\n+\tfor suffix in refs HEAD.log source.log destination.log\n+\tdo\n+\t\ttest_cmp \"$1.$suffix\" \"$2.$suffix\" || return 1\n+\tdone\n+}\n+\n+test_expect_success 'setup' '\n+\ttest_commit A &&\n+\ttest_commit B &&\n+\tA=$(git rev-parse A) &&\n+\tB=$(git rev-parse B)\n+'\n+\n+for operation in rename copy\n+do\n+\ttest_expect_success \"$operation reports one logical transaction\" '\n+\t\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\t\tgit branch -f source \"$A\" &&\n+\t\tgit branch -f destination \"$B\" &&\n+\t\tmkdir -p hooks &&\n+\t\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\t\techo \"$1\" >>actual &&\n+\t\t\tcat >>actual\n+\t\tEOF\n+\t\tgit config core.hooksPath hooks &&\n+\t\t>actual &&\n+\t\ttest-tool ref-store main copy-transaction \"$operation\" refs/heads/source refs/heads/destination &&\n+\t\t{\n+\t\t\techo preparing &&\n+\t\t\tif test \"$operation\" = rename\n+\t\t\tthen\n+\t\t\t\techo \"$ZERO_OID $ZERO_OID refs/heads/source\"\n+\t\t\tfi &&\n+\t\t\techo \"$ZERO_OID $A refs/heads/destination\" &&\n+\t\t\tfor state in prepared committed\n+\t\t\tdo\n+\t\t\t\techo \"$state\" &&\n+\t\t\t\tif test \"$operation\" = rename\n+\t\t\t\tthen\n+\t\t\t\t\techo \"$A $ZERO_OID refs/heads/source\"\n+\t\t\t\tfi &&\n+\t\t\t\techo \"$B $A refs/heads/destination\" || return 1\n+\t\t\tdone\n+\t\t} >expect &&\n+\t\ttest_cmp expect actual\n+\t'\n+\n+\tfor state in preparing prepared\n+\tdo\n+\t\ttest_expect_success \"$operation rejected at $state leaves refs and full reflogs unchanged\" '\n+\t\t\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\t\t\tgit branch -f source \"$A\" &&\n+\t\t\tgit branch -f destination \"$B\" &&\n+\t\t\tgit symbolic-ref HEAD refs/heads/source &&\n+\t\t\ttest_when_finished \"git -c core.hooksPath=/dev/null symbolic-ref HEAD refs/heads/main\" &&\n+\t\t\tsnapshot before &&\n+\t\t\twrite_script hooks/reference-transaction <<-EOF &&\n+\t\t\t\ttest \"\\$1\" != \"$state\"\n+\t\t\tEOF\n+\t\t\tgit config core.hooksPath hooks &&\n+\t\t\ttest_must_fail test-tool ref-store main copy-transaction \"$operation\" refs/heads/source refs/heads/destination &&\n+\t\t\tsnapshot after &&\n+\t\t\tcompare_snapshot before after\n+\t\t'\n+\tdone\n+done\n+\n+test_expect_success 'prepared hook sees old refs and cannot modify the source' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch -f source \"$A\" &&\n+\tgit branch -f destination \"$B\" &&\n+\twrite_script hooks/reference-transaction <<-EOF &&\n+\t\ttest \"\\$1\" = prepared || exit 0\n+\t\tgit rev-parse refs/heads/source >source-seen &&\n+\t\tgit rev-parse refs/heads/destination >destination-seen &&\n+\t\tif git -c core.hooksPath=/dev/null -c core.filesRefLockTimeout=0 \\\n+\t\t\tupdate-ref refs/heads/source $B\n+\t\tthen\n+\t\t\texit 1\n+\t\tfi\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\tgit branch -M source destination &&\n+\techo \"$A\" >expect &&\n+\ttest_cmp expect source-seen &&\n+\techo \"$B\" >expect &&\n+\ttest_cmp expect destination-seen &&\n+\ttest_cmp_rev \"$A\" destination\n+'\n+\n+test_expect_success 'preparing may update source and its reflog' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch source \"$A\" &&\n+\twrite_script hooks/reference-transaction <<-EOF &&\n+\t\ttest \"\\$1\" = preparing || exit 0\n+\t\tgit -c core.hooksPath=/dev/null update-ref -m concurrent refs/heads/source $B\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\tgit branch -M source destination &&\n+\ttest_cmp_rev \"$B\" destination &&\n+\tgit reflog show --format=%gs destination >actual &&\n+\ttest_grep concurrent actual\n+'\n+\n+test_expect_success 'rename without a source reflog can be rejected without losing destination history' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit -c core.logAllRefUpdates=false branch source \"$A\" &&\n+\ttest_must_fail git reflog exists refs/heads/source &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/destination >before &&\n+\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\ttest_must_fail git branch -M source destination &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/destination >after &&\n+\ttest_cmp before after\n+'\n+\n+for names in \"parent parent/child\" \"child/branch child\"\n+do\n+\tset -- $names\n+\tfrom=$1 to=$2\n+\ttest_expect_success \"D/F rename $from to $to can be aborted and committed\" '\n+\t\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\t\tgit branch \"$from\" \"$A\" &&\n+\t\ttest-tool ref-store main for-each-reflog-ent \"refs/heads/$from\" >before &&\n+\t\twrite_script hooks/reference-transaction <<-EOF &&\n+\t\t\ttest \"\\$1\" = prepared || exit 0\n+\t\t\tgit rev-parse refs/heads/$from >seen\n+\t\t\texit 1\n+\t\tEOF\n+\t\tgit config core.hooksPath hooks &&\n+\t\ttest_must_fail git branch -m \"$from\" \"$to\" &&\n+\t\techo \"$A\" >expect &&\n+\t\ttest_cmp expect seen &&\n+\t\ttest_cmp_rev \"$A\" \"$from\" &&\n+\t\ttest-tool ref-store main for-each-reflog-ent \"refs/heads/$from\" >after &&\n+\t\ttest_cmp before after &&\n+\t\tgit -c core.hooksPath=/dev/null branch -m \"$from\" \"$to\" &&\n+\t\ttest_cmp_rev \"$A\" \"$to\"\n+\t'\n+done\n+\n+test_expect_success REFFILES,POSIXPERM 'D/F reflog installation failure restores source history' '\n+\ttest_when_finished \"chmod u+w .git/logs/refs/heads\" &&\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch rollback/branch \"$A\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/rollback/branch >before &&\n+\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" = prepared || exit 0\n+\t\tchmod a-w .git/logs/refs/heads\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\ttest_must_fail git branch -m rollback/branch rollback &&\n+\tchmod u+w .git/logs/refs/heads &&\n+\ttest_cmp_rev \"$A\" rollback/branch &&\n+\ttest_must_fail git show-ref --verify refs/heads/rollback &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/rollback/branch >after &&\n+\ttest_cmp before after\n+'\n+\n+test_expect_success 'multiple renames, a copy and an ordinary update compose' '\n+\tgit branch first \"$A\" &&\n+\tgit branch second \"$B\" &&\n+\tgit branch third \"$A\" &&\n+\ttest-tool ref-store main copy-transaction \\\n+\t\trename refs/heads/first refs/heads/first-new \\\n+\t\trename refs/heads/second refs/heads/second-new \\\n+\t\tcopy refs/heads/third refs/heads/third-new \\\n+\t\tupdate refs/heads/fourth \"$B\" &&\n+\ttest_must_fail git show-ref --verify refs/heads/first &&\n+\ttest_must_fail git show-ref --verify refs/heads/second &&\n+\ttest_cmp_rev \"$A\" first-new &&\n+\ttest_cmp_rev \"$B\" second-new &&\n+\ttest_cmp_rev \"$A\" third-new &&\n+\ttest_cmp_rev \"$A\" third &&\n+\ttest_cmp_rev \"$B\" fourth\n+'\n+\n+test_expect_success 'copy preserves complete source history and replaces destination history' '\n+\tgit branch history-source \"$A\" &&\n+\tgit update-ref -m history-step refs/heads/history-source \"$B\" &&\n+\tgit branch history-destination \"$B\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/history-source >before &&\n+\tgit branch -C history-source history-destination &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/history-source >after &&\n+\ttest_cmp before after &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/history-destination >copied &&\n+\tsed \"$ d\" copied >history &&\n+\ttest_cmp before history &&\n+\ttest_grep \"Branch: copied refs/heads/history-source to refs/heads/history-destination\" copied\n+'\n+\n+test_expect_success 'packed rename reports no internal transactions' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch packed-source \"$A\" &&\n+\tgit pack-refs --all &&\n+\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\techo \"$1\" >>states\n+\t\tcat >/dev/null\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\tgit branch -m packed-source packed-destination &&\n+\tprintf \"%s\\n\" preparing prepared committed >expect &&\n+\ttest_cmp expect states\n+'\n+\n+test_expect_success REFFILES 'unrelated forced copies use independent reflog staging files' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch nested-a \"$A\" &&\n+\tgit branch nested-b \"$B\" &&\n+\tgit branch outer-a \"$A\" &&\n+\tgit branch outer-b \"$B\" &&\n+\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" = prepared || exit 0\n+\t\tgit -c core.hooksPath=/dev/null branch -C nested-a nested-b\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\tgit branch -C outer-a outer-b &&\n+\ttest_cmp_rev \"$A\" outer-b &&\n+\ttest_cmp_rev \"$A\" nested-b\n+'\n+\n+test_expect_success REFFILES 'rename with reflogs disabled does not create a reflog' '\n+\tgit -c core.logAllRefUpdates=false branch no-log \"$A\" &&\n+\tgit -c core.logAllRefUpdates=false branch -m no-log still-no-log &&\n+\ttest_must_fail git reflog exists refs/heads/still-no-log\n+'\n+\n+test_expect_success REFFILES 'copy without source history retains existing destination history' '\n+\tgit -c core.logAllRefUpdates=false branch no-copy-log \"$A\" &&\n+\tgit branch existing-copy-log \"$B\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/existing-copy-log >before &&\n+\tgit branch -C no-copy-log existing-copy-log &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/existing-copy-log >after &&\n+\tsed \"$ d\" after >history &&\n+\ttest_cmp before history\n+'\n+\n+test_expect_success REFFILES,CASE_INSENSITIVE_FS 'case-only rename retains history and supports abort' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch case-source \"$A\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/case-source >before &&\n+\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" != prepared\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\ttest_must_fail git branch -M case-source CASE-SOURCE &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/case-source >after &&\n+\ttest_cmp before after &&\n+\tgit -c core.hooksPath=/dev/null branch -M case-source CASE-SOURCE &&\n+\tgit for-each-ref --format=\"%(refname)\" refs/heads/CASE-SOURCE >actual &&\n+\techo refs/heads/CASE-SOURCE >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success REFFILES,SYMLINKS 'symlink source reflog is rejected without changing refs' '\n+\tgit branch symlink-source \"$A\" &&\n+\tmv .git/logs/refs/heads/symlink-source saved-log &&\n+\tln -s \"$PWD/saved-log\" .git/logs/refs/heads/symlink-source &&\n+\ttest_must_fail git branch -m symlink-source symlink-destination &&\n+\ttest_cmp_rev \"$A\" symlink-source &&\n+\ttest_must_fail git show-ref --verify refs/heads/symlink-destination\n+'\n+\n+for operation in copy rename\n+do\n+\ttest_expect_success \"$operation over HEAD referent preserves HEAD history\" '\n+\t\tgit branch -f head-source \"$A\" &&\n+\t\tgit update-ref refs/heads/main \"$B\" &&\n+\t\ttest-tool ref-store main for-each-reflog-ent HEAD >before &&\n+\t\ttest-tool ref-store main copy-transaction \"$operation\" refs/heads/head-source refs/heads/main &&\n+\t\ttest-tool ref-store main for-each-reflog-ent HEAD >after &&\n+\t\tsed \"$ d\" after >history &&\n+\t\ttest_cmp before history &&\n+\t\ttest_cmp_rev \"$A\" HEAD\n+\t'\n+done\n+\n+test_expect_success 'preparing may delete source but cannot cause a partial rename' '\n+\ttest_when_finished \"git config --unset core.hooksPath\" &&\n+\tgit branch race-source \"$A\" &&\n+\twrite_script hooks/reference-transaction <<-\\EOF &&\n+\t\ttest \"$1\" = preparing || exit 0\n+\t\tgit -c core.hooksPath=/dev/null update-ref -d refs/heads/race-source\n+\tEOF\n+\tgit config core.hooksPath hooks &&\n+\ttest_must_fail git branch -m race-source race-destination &&\n+\ttest_must_fail git show-ref --verify refs/heads/race-source &&\n+\ttest_must_fail git show-ref --verify refs/heads/race-destination\n+'\n+\n+test_expect_success 'copy cannot replace a D/F-conflicting source' '\n+\tgit branch conflict-source \"$A\" &&\n+\ttest_must_fail git branch -c conflict-source conflict-source/child &&\n+\ttest_cmp_rev \"$A\" conflict-source &&\n+\ttest_must_fail git show-ref --verify refs/heads/conflict-source/child\n+'\n+\n+test_expect_success REFFILES,POSIXPERM 'unreadable source reflog leaves refs and history unchanged' '\n+\tgit branch unreadable \"$A\" &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/unreadable >before &&\n+\ttest_when_finished \"chmod u+r .git/logs/refs/heads/unreadable\" &&\n+\tchmod a-r .git/logs/refs/heads/unreadable &&\n+\ttest_must_fail git branch -m unreadable readable &&\n+\tchmod u+r .git/logs/refs/heads/unreadable &&\n+\ttest_cmp_rev \"$A\" unreadable &&\n+\ttest_must_fail git show-ref --verify refs/heads/readable &&\n+\ttest-tool ref-store main for-each-reflog-ent refs/heads/unreadable >after &&\n+\ttest_cmp before after\n+'\n+\n+test_expect_success REFFILES 'destination lock failure preserves source and destination history' '\n+\tgit branch -f source \"$A\" &&\n+\tgit branch -f destination \"$B\" &&\n+\tsnapshot before &&\n+\ttest_when_finished \"rm -f .git/refs/heads/destination.lock\" &&\n+\t>.git/refs/heads/destination.lock &&\n+\ttest_must_fail git -c core.filesRefLockTimeout=0 branch -M source destination &&\n+\tsnapshot after &&\n+\tcompare_snapshot before after\n+'\n+\n+test_expect_success REFFILES 'staged reflog files are cleaned up after success and abort' '\n+\tfind .git/logs -name \".tmp-reflog-*\" >actual &&\n+\ttest_must_be_empty actual\n+'\n+\n+test_done\n-- \n2.39.3 (Apple Git-146)\n\n\n"},{"id":"554466","messageId":"f3f2c7ee11593be5e7f1f6668f4c601b6247c6e7.1791452597.git.maciej.ciemborowicz@gmail.com","threadId":"66350","inReplyTo":"cover.1791452597.git.maciej.ciemborowicz@gmail.com","subject":"[PATCH v4 4/4] refs: remove backend-specific copy and rename callbacks","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-08T09:44:19Z","receivedAt":"2026-10-08T09:44:19Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"Copy and rename now use the transaction API for refs and reflogs. Remove\nthe unused callbacks, backend implementations, and their private\nhelpers. The files backend no longer needs a shared temporary reflog\nname or rollback writes that reconstruct previously visible refs and\nlogs.\n\nSigned-off-by: Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com>\n---\n refs/debug.c            |  24 ---\n refs/files-backend.c    | 334 ----------------------------------------\n refs/packed-backend.c   |   2 -\n refs/refs-internal.h    |   9 --\n refs/reftable-backend.c | 287 ----------------------------------\n 5 files changed, 656 deletions(-)\n\ndiff --git a/refs/debug.c b/refs/debug.c\nindex 639db0f26e..b4991f71ae 100644\n--- a/refs/debug.c\n+++ b/refs/debug.c\n@@ -143,28 +143,6 @@ static int debug_optimize_required(struct ref_store *ref_store,\n \treturn res;\n }\n \n-static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,\n-\t\t\t    const char *newref, const char *logmsg)\n-{\n-\tstruct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;\n-\tint res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref,\n-\t\t\t\t\t      logmsg);\n-\ttrace_printf_key(&trace_refs, \"rename_ref: %s -> %s \\\"%s\\\": %d\\n\", oldref, newref,\n-\t\tlogmsg, res);\n-\treturn res;\n-}\n-\n-static int debug_copy_ref(struct ref_store *ref_store, const char *oldref,\n-\t\t\t  const char *newref, const char *logmsg)\n-{\n-\tstruct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;\n-\tint res =\n-\t\tdrefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg);\n-\ttrace_printf_key(&trace_refs, \"copy_ref: %s -> %s \\\"%s\\\": %d\\n\", oldref, newref,\n-\t\tlogmsg, res);\n-\treturn res;\n-}\n-\n struct debug_ref_iterator {\n \tstruct ref_iterator base;\n \tstruct ref_iterator *iter;\n@@ -453,8 +431,6 @@ struct ref_storage_be refs_be_debug = {\n \t.optimize = debug_optimize,\n \t.optimize_required = debug_optimize_required,\n \n-\t.rename_ref = debug_rename_ref,\n-\t.copy_ref = debug_copy_ref,\n \n \t.iterator_begin = debug_ref_iterator_begin,\n \t.read_raw_ref = debug_read_raw_ref,\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex ffdc112f9a..589e501050 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1623,273 +1623,6 @@ static int files_optimize_required(struct ref_store *ref_store,\n \treturn 0;\n }\n \n-/*\n- * People using contrib's git-new-workdir have .git/logs/refs ->\n- * /some/other/path/.git/logs/refs, and that may live on another device.\n- *\n- * IOW, to avoid cross device rename errors, the temporary renamed log must\n- * live into logs/refs.\n- */\n-#define TMP_RENAMED_LOG  \"refs/.tmp-renamed-log\"\n-\n-struct rename_cb {\n-\tconst char *tmp_renamed_log;\n-\tint true_errno;\n-};\n-\n-static int rename_tmp_log_callback(const char *path, void *cb_data)\n-{\n-\tstruct rename_cb *cb = cb_data;\n-\n-\tif (rename(cb->tmp_renamed_log, path)) {\n-\t\t/*\n-\t\t * rename(a, b) when b is an existing directory ought\n-\t\t * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.\n-\t\t * Sheesh. Record the true errno for error reporting,\n-\t\t * but report EISDIR to raceproof_create_file() so\n-\t\t * that it knows to retry.\n-\t\t */\n-\t\tcb->true_errno = errno;\n-\t\tif (errno == ENOTDIR)\n-\t\t\terrno = EISDIR;\n-\t\treturn -1;\n-\t} else {\n-\t\treturn 0;\n-\t}\n-}\n-\n-static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)\n-{\n-\tstruct strbuf path = STRBUF_INIT;\n-\tstruct strbuf tmp = STRBUF_INIT;\n-\tstruct rename_cb cb;\n-\tint ret;\n-\n-\tfiles_reflog_path(refs, &path, newrefname);\n-\tfiles_reflog_path(refs, &tmp, TMP_RENAMED_LOG);\n-\tcb.tmp_renamed_log = tmp.buf;\n-\tret = raceproof_create_file(refs, path.buf, rename_tmp_log_callback, &cb);\n-\tif (ret) {\n-\t\tif (errno == EISDIR)\n-\t\t\terror(\"directory not empty: %s\", path.buf);\n-\t\telse\n-\t\t\terror(\"unable to move logfile %s to %s: %s\",\n-\t\t\t      tmp.buf, path.buf,\n-\t\t\t      strerror(cb.true_errno));\n-\t}\n-\n-\tstrbuf_release(&path);\n-\tstrbuf_release(&tmp);\n-\treturn ret;\n-}\n-\n-static enum ref_transaction_error write_ref_to_lockfile(struct files_ref_store *refs,\n-\t\t\t\t\t\t\tstruct ref_lock *lock,\n-\t\t\t\t\t\t\tconst struct object_id *oid,\n-\t\t\t\t\t\t\tstruct strbuf *err);\n-static int commit_ref_update(struct files_ref_store *refs,\n-\t\t\t     struct ref_lock *lock,\n-\t\t\t     const struct object_id *oid, const char *logmsg,\n-\t\t\t     int flags,\n-\t\t\t     struct strbuf *err);\n-\n-/*\n- * Emit a better error message than lockfile.c's\n- * unable_to_lock_message() would in case there is a D/F conflict with\n- * another existing reference. If there would be a conflict, emit an error\n- * message and return false; otherwise, return true.\n- *\n- * Note that this function is not safe against all races with other\n- * processes, and that's not its job. We'll emit a more verbose error on D/f\n- * conflicts if we get past it into lock_ref_oid_basic().\n- */\n-static int refs_rename_ref_available(struct ref_store *refs,\n-\t\t\t      const char *old_refname,\n-\t\t\t      const char *new_refname)\n-{\n-\tstruct string_list skip = STRING_LIST_INIT_NODUP;\n-\tstruct strbuf err = STRBUF_INIT;\n-\tint ok;\n-\n-\tstring_list_insert(&skip, old_refname);\n-\tok = !refs_verify_refname_available(refs, new_refname,\n-\t\t\t\t\t    NULL, &skip, 0, &err);\n-\tif (!ok)\n-\t\terror(\"%s\", err.buf);\n-\n-\tstring_list_clear(&skip, 0);\n-\tstrbuf_release(&err);\n-\treturn ok;\n-}\n-\n-static int files_copy_or_rename_ref(struct ref_store *ref_store,\n-\t\t\t    const char *oldrefname, const char *newrefname,\n-\t\t\t    const char *logmsg, int copy)\n-{\n-\tstruct files_ref_store *refs =\n-\t\tfiles_downcast(ref_store, REF_STORE_WRITE, \"rename_ref\");\n-\tstruct object_id orig_oid;\n-\tint flag = 0, logmoved = 0;\n-\tstruct ref_lock *lock;\n-\tstruct stat loginfo;\n-\tstruct strbuf sb_oldref = STRBUF_INIT;\n-\tstruct strbuf sb_newref = STRBUF_INIT;\n-\tstruct strbuf tmp_renamed_log = STRBUF_INIT;\n-\tint log, ret;\n-\tstruct strbuf err = STRBUF_INIT;\n-\n-\tfiles_reflog_path(refs, &sb_oldref, oldrefname);\n-\tfiles_reflog_path(refs, &sb_newref, newrefname);\n-\tfiles_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);\n-\n-\tlog = !lstat(sb_oldref.buf, &loginfo);\n-\tif (log && S_ISLNK(loginfo.st_mode)) {\n-\t\tret = error(\"reflog for %s is a symlink\", oldrefname);\n-\t\tgoto out;\n-\t}\n-\n-\tif (!refs_resolve_ref_unsafe(&refs->base, oldrefname,\n-\t\t\t\t     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n-\t\t\t\t     &orig_oid, &flag)) {\n-\t\tret = error(\"refname %s not found\", oldrefname);\n-\t\tgoto out;\n-\t}\n-\n-\tif (flag & REF_ISSYMREF) {\n-\t\tif (copy)\n-\t\t\tret = error(\"refname %s is a symbolic ref, copying it is not supported\",\n-\t\t\t\t    oldrefname);\n-\t\telse\n-\t\t\tret = error(\"refname %s is a symbolic ref, renaming it is not supported\",\n-\t\t\t\t    oldrefname);\n-\t\tgoto out;\n-\t}\n-\tif (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {\n-\t\tret = 1;\n-\t\tgoto out;\n-\t}\n-\n-\tif (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {\n-\t\tret = error(\"unable to move logfile logs/%s to logs/\"TMP_RENAMED_LOG\": %s\",\n-\t\t\t    oldrefname, strerror(errno));\n-\t\tgoto out;\n-\t}\n-\n-\tif (copy && log && copy_file(refs->base.repo, tmp_renamed_log.buf, sb_oldref.buf, 0644)) {\n-\t\tret = error(\"unable to copy logfile logs/%s to logs/\"TMP_RENAMED_LOG\": %s\",\n-\t\t\t    oldrefname, strerror(errno));\n-\t\tgoto out;\n-\t}\n-\n-\tif (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,\n-\t\t\t    &orig_oid, REF_NO_DEREF)) {\n-\t\terror(\"unable to delete old %s\", oldrefname);\n-\t\tgoto rollback;\n-\t}\n-\n-\t/*\n-\t * Since we are doing a shallow lookup, oid is not the\n-\t * correct value to pass to delete_ref as old_oid. But that\n-\t * doesn't matter, because an old_oid check wouldn't add to\n-\t * the safety anyway; we want to delete the reference whatever\n-\t * its current value.\n-\t */\n-\tif (!copy && refs_resolve_ref_unsafe(&refs->base, newrefname,\n-\t\t\t\t\t     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,\n-\t\t\t\t\t     NULL, NULL) &&\n-\t    refs_delete_ref(&refs->base, NULL, newrefname,\n-\t\t\t    NULL, REF_NO_DEREF)) {\n-\t\tif (errno == EISDIR) {\n-\t\t\tstruct strbuf path = STRBUF_INIT;\n-\t\t\tint result;\n-\n-\t\t\tfiles_ref_path(refs, &path, newrefname);\n-\t\t\tresult = remove_empty_directories(&path);\n-\t\t\tstrbuf_release(&path);\n-\n-\t\t\tif (result) {\n-\t\t\t\terror(\"Directory not empty: %s\", newrefname);\n-\t\t\t\tgoto rollback;\n-\t\t\t}\n-\t\t} else {\n-\t\t\terror(\"unable to delete existing %s\", newrefname);\n-\t\t\tgoto rollback;\n-\t\t}\n-\t}\n-\n-\tif (log && rename_tmp_log(refs, newrefname))\n-\t\tgoto rollback;\n-\n-\tlogmoved = log;\n-\n-\tlock = lock_ref_oid_basic(refs, newrefname, &err);\n-\tif (!lock) {\n-\t\tif (copy)\n-\t\t\terror(\"unable to copy '%s' to '%s': %s\", oldrefname, newrefname, err.buf);\n-\t\telse\n-\t\t\terror(\"unable to rename '%s' to '%s': %s\", oldrefname, newrefname, err.buf);\n-\t\tstrbuf_release(&err);\n-\t\tgoto rollback;\n-\t}\n-\toidcpy(&lock->old_oid, &orig_oid);\n-\n-\tif (write_ref_to_lockfile(refs, lock, &orig_oid, &err) ||\n-\t    commit_ref_update(refs, lock, &orig_oid, logmsg, 0, &err)) {\n-\t\terror(\"unable to write current sha1 into %s: %s\", newrefname, err.buf);\n-\t\tstrbuf_release(&err);\n-\t\tgoto rollback;\n-\t}\n-\n-\tret = 0;\n-\tgoto out;\n-\n- rollback:\n-\tlock = lock_ref_oid_basic(refs, oldrefname, &err);\n-\tif (!lock) {\n-\t\terror(\"unable to lock %s for rollback: %s\", oldrefname, err.buf);\n-\t\tstrbuf_release(&err);\n-\t\tgoto rollbacklog;\n-\t}\n-\n-\tif (write_ref_to_lockfile(refs, lock, &orig_oid, &err) ||\n-\t    commit_ref_update(refs, lock, &orig_oid, NULL, REF_SKIP_CREATE_REFLOG, &err)) {\n-\t\terror(\"unable to write current sha1 into %s: %s\", oldrefname, err.buf);\n-\t\tstrbuf_release(&err);\n-\t}\n-\n- rollbacklog:\n-\tif (logmoved && rename(sb_newref.buf, sb_oldref.buf))\n-\t\terror(\"unable to restore logfile %s from %s: %s\",\n-\t\t\toldrefname, newrefname, strerror(errno));\n-\tif (!logmoved && log &&\n-\t    rename(tmp_renamed_log.buf, sb_oldref.buf))\n-\t\terror(\"unable to restore logfile %s from logs/\"TMP_RENAMED_LOG\": %s\",\n-\t\t\toldrefname, strerror(errno));\n-\tret = 1;\n- out:\n-\tstrbuf_release(&sb_newref);\n-\tstrbuf_release(&sb_oldref);\n-\tstrbuf_release(&tmp_renamed_log);\n-\n-\treturn ret;\n-}\n-\n-static int files_rename_ref(struct ref_store *ref_store,\n-\t\t\t    const char *oldrefname, const char *newrefname,\n-\t\t\t    const char *logmsg)\n-{\n-\treturn files_copy_or_rename_ref(ref_store, oldrefname,\n-\t\t\t\t newrefname, logmsg, 0);\n-}\n-\n-static int files_copy_ref(struct ref_store *ref_store,\n-\t\t\t    const char *oldrefname, const char *newrefname,\n-\t\t\t    const char *logmsg)\n-{\n-\treturn files_copy_or_rename_ref(ref_store, oldrefname,\n-\t\t\t\t newrefname, logmsg, 1);\n-}\n-\n static int close_ref_gently(struct ref_lock *lock)\n {\n \tif (close_lock_file_gently(&lock->lk))\n@@ -2121,71 +1854,6 @@ static enum ref_transaction_error write_ref_to_lockfile(struct files_ref_store *\n \treturn 0;\n }\n \n-/*\n- * Commit a change to a loose reference that has already been written\n- * to the loose reference lockfile. Also update the reflogs if\n- * necessary, using the specified lockmsg (which can be NULL).\n- */\n-static int commit_ref_update(struct files_ref_store *refs,\n-\t\t\t     struct ref_lock *lock,\n-\t\t\t     const struct object_id *oid, const char *logmsg,\n-\t\t\t     int flags,\n-\t\t\t     struct strbuf *err)\n-{\n-\tfiles_assert_main_repository(refs, \"commit_ref_update\");\n-\n-\tclear_loose_ref_cache(refs);\n-\tif (files_log_ref_write(refs, lock->ref_name, &lock->old_oid, oid, NULL,\n-\t\t\t\tlogmsg, flags, err)) {\n-\t\tchar *old_msg = strbuf_detach(err, NULL);\n-\t\tstrbuf_addf(err, \"cannot update the ref '%s': %s\",\n-\t\t\t    lock->ref_name, old_msg);\n-\t\tfree(old_msg);\n-\t\tunlock_ref(lock);\n-\t\treturn -1;\n-\t}\n-\n-\tif (strcmp(lock->ref_name, \"HEAD\") != 0) {\n-\t\t/*\n-\t\t * Special hack: If a branch is updated directly and HEAD\n-\t\t * points to it (may happen on the remote side of a push\n-\t\t * for example) then logically the HEAD reflog should be\n-\t\t * updated too.\n-\t\t * A generic solution implies reverse symref information,\n-\t\t * but finding all symrefs pointing to the given branch\n-\t\t * would be rather costly for this rare event (the direct\n-\t\t * update of a branch) to be worth it.  So let's cheat and\n-\t\t * check with HEAD only which should cover 99% of all usage\n-\t\t * scenarios (even 100% of the default ones).\n-\t\t */\n-\t\tint head_flag;\n-\t\tconst char *head_ref;\n-\n-\t\thead_ref = refs_resolve_ref_unsafe(&refs->base, \"HEAD\",\n-\t\t\t\t\t\t   RESOLVE_REF_READING,\n-\t\t\t\t\t\t   NULL, &head_flag);\n-\t\tif (head_ref && (head_flag & REF_ISSYMREF) &&\n-\t\t    !strcmp(head_ref, lock->ref_name)) {\n-\t\t\tstruct strbuf log_err = STRBUF_INIT;\n-\t\t\tif (files_log_ref_write(refs, \"HEAD\", &lock->old_oid,\n-\t\t\t\t\t\toid, NULL, logmsg, flags,\n-\t\t\t\t\t\t&log_err)) {\n-\t\t\t\terror(\"%s\", log_err.buf);\n-\t\t\t\tstrbuf_release(&log_err);\n-\t\t\t}\n-\t\t}\n-\t}\n-\n-\tif (commit_ref(lock)) {\n-\t\tstrbuf_addf(err, \"couldn't set '%s'\", lock->ref_name);\n-\t\tunlock_ref(lock);\n-\t\treturn -1;\n-\t}\n-\n-\tunlock_ref(lock);\n-\treturn 0;\n-}\n-\n #if defined(NO_SYMLINK_HEAD) || defined(WITH_BREAKING_CHANGES)\n #define create_ref_symlink(a, b) (-1)\n #else\n@@ -4471,8 +4139,6 @@ struct ref_storage_be refs_be_files = {\n \n \t.optimize = files_optimize,\n \t.optimize_required = files_optimize_required,\n-\t.rename_ref = files_rename_ref,\n-\t.copy_ref = files_copy_ref,\n \n \t.iterator_begin = files_ref_iterator_begin,\n \t.read_raw_ref = files_read_raw_ref,\ndiff --git a/refs/packed-backend.c b/refs/packed-backend.c\nindex a73fc6aca7..364a912912 100644\n--- a/refs/packed-backend.c\n+++ b/refs/packed-backend.c\n@@ -2164,8 +2164,6 @@ struct ref_storage_be refs_be_packed = {\n \t.optimize = packed_optimize,\n \t.optimize_required = packed_optimize_required,\n \n-\t.rename_ref = NULL,\n-\t.copy_ref = NULL,\n \n \t.iterator_begin = packed_ref_iterator_begin,\n \t.read_raw_ref = packed_read_raw_ref,\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex 2746ce5b49..c415ba0a4e 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -470,13 +470,6 @@ typedef int optimize_required_fn(struct ref_store *ref_store,\n \t\t\t\t struct refs_optimize_opts *opts,\n \t\t\t\t bool *required);\n \n-typedef int rename_ref_fn(struct ref_store *ref_store,\n-\t\t\t  const char *oldref, const char *newref,\n-\t\t\t  const char *logmsg);\n-typedef int copy_ref_fn(struct ref_store *ref_store,\n-\t\t\t  const char *oldref, const char *newref,\n-\t\t\t  const char *logmsg);\n-\n /*\n  * Iterate over the references in `ref_store` whose names start with\n  * `prefix`. `prefix` is matched as a literal string, without regard\n@@ -596,8 +589,6 @@ struct ref_storage_be {\n \n \toptimize_fn *optimize;\n \toptimize_required_fn *optimize_required;\n-\trename_ref_fn *rename_ref;\n-\tcopy_ref_fn *copy_ref;\n \n \tref_iterator_begin_fn *iterator_begin;\n \tread_raw_ref_fn *read_raw_ref;\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex 72c1596c11..ef3bab69ee 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -1798,290 +1798,6 @@ struct write_create_symref_arg {\n \tconst char *logmsg;\n };\n \n-struct write_copy_arg {\n-\tstruct reftable_ref_store *refs;\n-\tstruct reftable_backend *be;\n-\tconst char *oldname;\n-\tconst char *newname;\n-\tconst char *logmsg;\n-\tint delete_old;\n-};\n-\n-static int write_copy_table(struct reftable_writer *writer, void *cb_data)\n-{\n-\tstruct write_copy_arg *arg = cb_data;\n-\tuint64_t deletion_ts, creation_ts;\n-\tstruct reftable_ref_record old_ref = {0}, refs[2] = {0};\n-\tstruct reftable_log_record old_log = {0}, *logs = NULL;\n-\tstruct reftable_iterator it = {0};\n-\tstruct string_list skip = STRING_LIST_INIT_NODUP;\n-\tstruct ident_split committer_ident = {0};\n-\tstruct strbuf errbuf = STRBUF_INIT;\n-\tsize_t logs_nr = 0, logs_alloc = 0, i;\n-\tconst char *committer_info;\n-\tint ret;\n-\n-\tcommitter_info = git_committer_info(0);\n-\tif (split_ident_line(&committer_ident, committer_info, strlen(committer_info)))\n-\t\tBUG(\"failed splitting committer info\");\n-\n-\tif (reftable_stack_read_ref(arg->be->stack, arg->oldname, &old_ref)) {\n-\t\tret = error(_(\"refname %s not found\"), arg->oldname);\n-\t\tgoto done;\n-\t}\n-\tif (old_ref.value_type == REFTABLE_REF_SYMREF) {\n-\t\tret = error(_(\"refname %s is a symbolic ref, copying it is not supported\"),\n-\t\t\t    arg->oldname);\n-\t\tgoto done;\n-\t}\n-\n-\t/*\n-\t * There's nothing to do in case the old and new name are the same, so\n-\t * we exit early in that case.\n-\t */\n-\tif (!strcmp(arg->oldname, arg->newname)) {\n-\t\tret = 0;\n-\t\tgoto done;\n-\t}\n-\n-\t/*\n-\t * Verify that the new refname is available.\n-\t */\n-\tif (arg->delete_old)\n-\t\tstring_list_insert(&skip, arg->oldname);\n-\tret = refs_verify_refname_available(&arg->refs->base, arg->newname,\n-\t\t\t\t\t    NULL, &skip, 0, &errbuf);\n-\tif (ret < 0) {\n-\t\terror(\"%s\", errbuf.buf);\n-\t\tgoto done;\n-\t}\n-\n-\t/*\n-\t * When deleting the old reference we have to use two update indices:\n-\t * once to delete the old ref and its reflog, and once to create the\n-\t * new ref and its reflog. They need to be staged with two separate\n-\t * indices because the new reflog needs to encode both the deletion of\n-\t * the old branch and the creation of the new branch, and we cannot do\n-\t * two changes to a reflog in a single update.\n-\t */\n-\tdeletion_ts = creation_ts = reftable_stack_next_update_index(arg->be->stack);\n-\tif (arg->delete_old)\n-\t\tcreation_ts++;\n-\tret = reftable_writer_set_limits(writer, deletion_ts, creation_ts);\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-\t/*\n-\t * Add the new reference. If this is a rename then we also delete the\n-\t * old reference.\n-\t */\n-\trefs[0] = old_ref;\n-\trefs[0].refname = xstrdup(arg->newname);\n-\trefs[0].update_index = creation_ts;\n-\tif (arg->delete_old) {\n-\t\trefs[1].refname = xstrdup(arg->oldname);\n-\t\trefs[1].value_type = REFTABLE_REF_DELETION;\n-\t\trefs[1].update_index = deletion_ts;\n-\t}\n-\tret = reftable_writer_add_refs(writer, refs, arg->delete_old ? 2 : 1);\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-\t/*\n-\t * When deleting the old branch we need to create a reflog entry on the\n-\t * new branch name that indicates that the old branch has been deleted\n-\t * and then recreated. This is a tad weird, but matches what the files\n-\t * backend does.\n-\t */\n-\tif (arg->delete_old) {\n-\t\tstruct strbuf head_referent = STRBUF_INIT;\n-\t\tstruct object_id head_oid;\n-\t\tint append_head_reflog;\n-\t\tunsigned head_type = 0;\n-\n-\t\tALLOC_GROW(logs, logs_nr + 1, logs_alloc);\n-\t\tmemset(&logs[logs_nr], 0, sizeof(logs[logs_nr]));\n-\t\tfill_reftable_log_record(&logs[logs_nr], &committer_ident);\n-\t\tlogs[logs_nr].refname = xstrdup(arg->newname);\n-\t\tlogs[logs_nr].update_index = deletion_ts;\n-\t\tlogs[logs_nr].value.update.message =\n-\t\t\txstrndup(arg->logmsg, reftable_be_write_options(arg->refs)->opts.block_size / 2);\n-\t\tmemcpy(logs[logs_nr].value.update.old_hash, old_ref.value.val1, GIT_MAX_RAWSZ);\n-\t\tlogs_nr++;\n-\n-\t\tret = reftable_backend_read_ref(arg->be, \"HEAD\", &head_oid,\n-\t\t\t\t\t\t&head_referent, &head_type);\n-\t\tif (ret < 0)\n-\t\t\tgoto done;\n-\t\tappend_head_reflog = (head_type & REF_ISSYMREF) && !strcmp(head_referent.buf, arg->oldname);\n-\t\tstrbuf_release(&head_referent);\n-\n-\t\t/*\n-\t\t * The files backend uses `refs_delete_ref()` to delete the old\n-\t\t * branch name, which will append a reflog entry for HEAD in\n-\t\t * case it points to the old branch.\n-\t\t */\n-\t\tif (append_head_reflog) {\n-\t\t\tALLOC_GROW(logs, logs_nr + 1, logs_alloc);\n-\t\t\tlogs[logs_nr] = logs[logs_nr - 1];\n-\t\t\tlogs[logs_nr].refname = xstrdup(\"HEAD\");\n-\t\t\tlogs[logs_nr].value.update.name =\n-\t\t\t\txstrdup(logs[logs_nr].value.update.name);\n-\t\t\tlogs[logs_nr].value.update.email =\n-\t\t\t\txstrdup(logs[logs_nr].value.update.email);\n-\t\t\tlogs[logs_nr].value.update.message =\n-\t\t\t\txstrdup(logs[logs_nr].value.update.message);\n-\t\t\tlogs_nr++;\n-\t\t}\n-\t}\n-\n-\t/*\n-\t * Create the reflog entry for the newly created branch.\n-\t */\n-\tALLOC_GROW(logs, logs_nr + 1, logs_alloc);\n-\tmemset(&logs[logs_nr], 0, sizeof(logs[logs_nr]));\n-\tfill_reftable_log_record(&logs[logs_nr], &committer_ident);\n-\tlogs[logs_nr].refname = xstrdup(arg->newname);\n-\tlogs[logs_nr].update_index = creation_ts;\n-\tlogs[logs_nr].value.update.message =\n-\t\txstrndup(arg->logmsg, reftable_be_write_options(arg->refs)->opts.block_size / 2);\n-\tmemcpy(logs[logs_nr].value.update.new_hash, old_ref.value.val1, GIT_MAX_RAWSZ);\n-\tlogs_nr++;\n-\n-\t/*\n-\t * In addition to writing the reflog entry for the new branch, we also\n-\t * copy over all log entries from the old reflog. Last but not least,\n-\t * when renaming we also have to delete all the old reflog entries.\n-\t */\n-\tret = reftable_stack_init_log_iterator(arg->be->stack, &it);\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-\tret = reftable_iterator_seek_log(&it, arg->oldname);\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-\twhile (1) {\n-\t\tret = reftable_iterator_next_log(&it, &old_log);\n-\t\tif (ret < 0)\n-\t\t\tgoto done;\n-\t\tif (ret > 0 || strcmp(old_log.refname, arg->oldname)) {\n-\t\t\tret = 0;\n-\t\t\tbreak;\n-\t\t}\n-\t\tif (reftable_log_record_is_deletion(&old_log))\n-\t\t\tcontinue;\n-\n-\t\tfree(old_log.refname);\n-\n-\t\t/*\n-\t\t * Copy over the old reflog entry with the new refname.\n-\t\t */\n-\t\tALLOC_GROW(logs, logs_nr + 1, logs_alloc);\n-\t\tlogs[logs_nr] = old_log;\n-\t\tlogs[logs_nr].refname = xstrdup(arg->newname);\n-\t\tlogs_nr++;\n-\n-\t\t/*\n-\t\t * Delete the old reflog entry in case we are renaming.\n-\t\t */\n-\t\tif (arg->delete_old) {\n-\t\t\tALLOC_GROW(logs, logs_nr + 1, logs_alloc);\n-\t\t\tmemset(&logs[logs_nr], 0, sizeof(logs[logs_nr]));\n-\t\t\tlogs[logs_nr].refname = xstrdup(arg->oldname);\n-\t\t\tlogs[logs_nr].value_type = REFTABLE_LOG_DELETION;\n-\t\t\tlogs[logs_nr].update_index = old_log.update_index;\n-\t\t\tlogs_nr++;\n-\t\t}\n-\n-\t\t/*\n-\t\t * Transfer ownership of the log record we're iterating over to\n-\t\t * the array of log records. Otherwise, the pointers would get\n-\t\t * free'd or reallocated by the iterator.\n-\t\t */\n-\t\tmemset(&old_log, 0, sizeof(old_log));\n-\t}\n-\n-\tret = reftable_writer_add_logs(writer, logs, logs_nr);\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-done:\n-\tassert(ret != REFTABLE_API_ERROR);\n-\treftable_iterator_destroy(&it);\n-\tstring_list_clear(&skip, 0);\n-\tstrbuf_release(&errbuf);\n-\tfor (i = 0; i < logs_nr; i++)\n-\t\treftable_log_record_release(&logs[i]);\n-\tfree(logs);\n-\tfor (i = 0; i < ARRAY_SIZE(refs); i++)\n-\t\treftable_ref_record_release(&refs[i]);\n-\treftable_ref_record_release(&old_ref);\n-\treftable_log_record_release(&old_log);\n-\treturn ret;\n-}\n-\n-static int reftable_be_rename_ref(struct ref_store *ref_store,\n-\t\t\t\t  const char *oldrefname,\n-\t\t\t\t  const char *newrefname,\n-\t\t\t\t  const char *logmsg)\n-{\n-\tstruct reftable_ref_store *refs =\n-\t\treftable_be_downcast(ref_store, REF_STORE_WRITE, \"rename_ref\");\n-\tstruct write_copy_arg arg = {\n-\t\t.refs = refs,\n-\t\t.oldname = oldrefname,\n-\t\t.newname = newrefname,\n-\t\t.logmsg = logmsg,\n-\t\t.delete_old = 1,\n-\t};\n-\tint ret;\n-\n-\tret = refs->err;\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-\tret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);\n-\tif (ret)\n-\t\tgoto done;\n-\tret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,\n-\t\t\t\t &reftable_be_write_options(refs)->opts);\n-\n-done:\n-\tassert(ret != REFTABLE_API_ERROR);\n-\treturn ret;\n-}\n-\n-static int reftable_be_copy_ref(struct ref_store *ref_store,\n-\t\t\t\tconst char *oldrefname,\n-\t\t\t\tconst char *newrefname,\n-\t\t\t\tconst char *logmsg)\n-{\n-\tstruct reftable_ref_store *refs =\n-\t\treftable_be_downcast(ref_store, REF_STORE_WRITE, \"copy_ref\");\n-\tstruct write_copy_arg arg = {\n-\t\t.refs = refs,\n-\t\t.oldname = oldrefname,\n-\t\t.newname = newrefname,\n-\t\t.logmsg = logmsg,\n-\t};\n-\tint ret;\n-\n-\tret = refs->err;\n-\tif (ret < 0)\n-\t\tgoto done;\n-\n-\tret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);\n-\tif (ret)\n-\t\tgoto done;\n-\tret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,\n-\t\t\t\t &reftable_be_write_options(refs)->opts);\n-\n-done:\n-\tassert(ret != REFTABLE_API_ERROR);\n-\treturn ret;\n-}\n-\n struct reftable_reflog_iterator {\n \tstruct ref_iterator base;\n \tstruct reftable_ref_store *refs;\n@@ -2909,9 +2625,6 @@ struct ref_storage_be refs_be_reftable = {\n \t.optimize = reftable_be_optimize,\n \t.optimize_required = reftable_be_optimize_required,\n \n-\t.rename_ref = reftable_be_rename_ref,\n-\t.copy_ref = reftable_be_copy_ref,\n-\n \t.iterator_begin = reftable_be_iterator_begin,\n \t.read_raw_ref = reftable_be_read_raw_ref,\n \t.read_symbolic_ref = reftable_be_read_symbolic_ref,\n-- \n2.39.3 (Apple Git-146)\n\n\n"},{"id":"554482","messageId":"asdsIjNEUOpaAnX5@pks.im","threadId":"66350","inReplyTo":"cover.1791452597.git.maciej.ciemborowicz@gmail.com","subject":"Re: [PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-08T10:10:42Z","receivedAt":"2026-10-08T10:10:42Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Oct 08, 2026 at 11:44:15AM +0200, Maciej Ciemborowicz wrote:\n> Changes since v3:\n> \n> * Rebase onto 6de20f6092 (The 4th batch, 2026-10-06), the master commit\n>   used in Junio's report.\n> * Preserve the packed preparation error in patch 2 as described above.\n> * Register t1425 and t1424 in t/meson.build in the commits adding them.\n\nPlease engage with the reviewers. Just posting new versions without\nreplying to them at all will very likely not get you anywhere. This kind\nof behaviour is nowadays a red flag and often hints at contributors who\nare basically just a meat proxy. And as a consequence, reviewers are\nvery likely to disengage and stop reviewing your patch series\naltogether, which is frustrating to everyone involved.\n\nPatrick\n\n"},{"id":"554483","messageId":"CACQ=SRGtpYLCcAaJz+yUR564wTm8wsMy1qn7hZ_iJfDTc_KTeQ@mail.gmail.com","threadId":"66350","inReplyTo":"asdsIjNEUOpaAnX5@pks.im","subject":"Re: [PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-08T10:43:31Z","receivedAt":"2026-10-08T10:43:31Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"On Thu, Oct 8, 2026 at 12:10 PM Patrick Steinhardt <ps@pks.im> wrote:\n\n> Please engage with the reviewers. Just posting new versions without\n> replying to them at all will very likely not get you anywhere. This kind\n> of behaviour is nowadays a red flag and often hints at contributors who\n> are basically just a meat proxy. And as a consequence, reviewers are\n> very likely to disengage and stop reviewing your patch series\n> altogether, which is frustrating to everyone involved.\n\nI reproduced both failures. In v3, preparing the reflogs overwrote an\nearlier packed-refs preparation error with success. V4 jumps to\ncleanup on that error.\nI had not run t0600 or t5510 before submitting v3. Both now pass\nunchanged, and the full standard test suite passes on macOS with both\nfiles and reftable, based on 6de20f6092. Sorry for missing this before\nsubmission.\n\nThanks,\nMaciej\n\n"},{"id":"554484","messageId":"CACQ=SRGOdtUvxDEBeXz93nrC01oRaTALx6EztyBEfuCtnmTk-Q@mail.gmail.com","threadId":"66350","inReplyTo":"CACQ=SRGtpYLCcAaJz+yUR564wTm8wsMy1qn7hZ_iJfDTc_KTeQ@mail.gmail.com","subject":"Re: [PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-08T11:01:10Z","receivedAt":"2026-10-08T11:01:10Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"On Thu, Oct 8, 2026 at 12:10 PM Patrick Steinhardt <ps@pks.im> wrote:\n> And as a consequence, reviewers are very likely to disengage and stop reviewing your patch series altogether, which is frustrating to everyone involved.\n\nI also read Notes from the Git Contributor's Summit yesterday, so I\nunderstand the problem, and I'm curious to see how things develop. I\ntry to respect the people reviewing code and avoid being just a \"meat\nproxy\", but I increasingly feel like I'm becoming one myself. Month\nafter month, AI is doing more and more of my work, and it's simply\nbetter at it than I am. If I had written all of this by hand, first,\nit would have taken me a month, and second, I would have made far more\nmistakes than AI does.\n\nI understand that the patch is large, and I'm concerned that this\ncould be a barrier to code review and discourage people from reading\nit and eventually merging it. After all, the more code there is, the\nharder it becomes to maintain. If you don't have the time to review\nit, I completely understand. In that case, I'll wait for someone else\nto solve the problem, as I suspect it will come up again on the\nmailing list at some point in the future.\n\nThanks,\nMaciej\n\n"},{"id":"554509","messageId":"xmqqqzi02o22.fsf@gitster.g","threadId":"66350","inReplyTo":"CACQ=SRGOdtUvxDEBeXz93nrC01oRaTALx6EztyBEfuCtnmTk-Q@mail.gmail.com","subject":"Re: [PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-08T15:45:57Z","receivedAt":"2026-10-08T15:45:57Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Maciej Ciemborowicz <maciej.ciemborowicz@gmail.com> writes:\n\n> better at it than I am. If I had written all of this by hand, first,\n> it would have taken me a month, and second, I would have made far more\n> mistakes than AI does.\n\nAnd thanks to your learning, the next patch series you will write\nwould be of much higher quality.\n\n> I understand that the patch is large, and I'm concerned that this\n> could be a barrier to code review and discourage people from reading\n\nLarge is not a problem; unnecessarily large is.\n\nCode generated by an LLM tends to be unnecessarily large, replete\nwith poor refactorings and outright repetitions of existing code.\nThis often shows that insufficient thought went into crafting the\nfinal result.  Perhaps this is unavoidable, since they do not really\nthink.\n\n\n"},{"id":"554511","messageId":"xmqqece02no9.fsf@gitster.g","threadId":"66350","inReplyTo":"asdsIjNEUOpaAnX5@pks.im","subject":"Re: [PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-08T15:54:14Z","receivedAt":"2026-10-08T15:54:14Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> On Thu, Oct 08, 2026 at 11:44:15AM +0200, Maciej Ciemborowicz wrote:\n>> Changes since v3:\n>> \n>> * Rebase onto 6de20f6092 (The 4th batch, 2026-10-06), the master commit\n>>   used in Junio's report.\n>> * Preserve the packed preparation error in patch 2 as described above.\n>> * Register t1425 and t1424 in t/meson.build in the commits adding them.\n>\n> Please engage with the reviewers. Just posting new versions without\n> replying to them at all will very likely not get you anywhere. This kind\n> of behaviour is nowadays a red flag and often hints at contributors who\n> are basically just a meat proxy. And as a consequence, reviewers are\n> very likely to disengage and stop reviewing your patch series\n> altogether, which is frustrating to everyone involved.\n\nThanks for bringing this up.\n\nA response to reviews on the N-th round must come long before\nsending the v(N+1) round of patches.  Some contributors send them\nafter v(N+1), or immediately before, but the proper time to respond\nis soon after receiving the reviews on vN and having had enough time\nto understand the comments, before starting work on v(N+1).  Only\nafter that work is complete would you send the new patches.  Hence,\nwe expect the time between vN and v(N+1) from real contributors to\nbe measured in days, not hours.  Whenever I see vN responses arrive\nafter or immediately before the v(N+1) patches, or worse, no\nresponse at all but just the new patches, it smells fishy.\n\n"},{"id":"554523","messageId":"CACQ=SRHRp4vKpV5JagAXS1n4iX-LJ7fm78OwMGB5fke2CTC4FQ@mail.gmail.com","threadId":"66350","inReplyTo":"xmqqqzi02o22.fsf@gitster.g","subject":"Re: [PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-08T19:06:32Z","receivedAt":"2026-10-08T19:06:32Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"On Thu, Oct 8, 2026 at 5:46 PM Junio C Hamano <gitster@pobox.com> wrote:\n\n> A response to reviews on the N-th round must come long before\n> sending the v(N+1) round of patches.  Some contributors send them\n> after v(N+1), or immediately before, but the proper time to respond\n> is soon after receiving the reviews on vN and having had enough time\n> to understand the comments, before starting work on v(N+1).  Only\n> after that work is complete would you send the new patches.  Hence,\n> we expect the time between vN and v(N+1) from real contributors to\n> be measured in days, not hours.  Whenever I see vN responses arrive\n> after or immediately before the v(N+1) patches, or worse, no\n> response at all but just the new patches, it smells fishy.\n\nI wouldn't rely on that. It's easy to fake. I'd rather ask for transparency.\n\n> And thanks to your learning, the next patch series you will write would be of much higher quality.\n\nTL;DR: I'm facing a moral dilemma: should I refrain from submitting a\npatch, or submit one written with the help of AI? If what I'm\nsubmitting isn't useful, feel free to say so or simply ignore the\npatch. If it has value and people are willing to accept it, I'm happy\nto continue.\n\nMy current workflow looks like this:\n\n1. I give an AI agent a task.\n2. I check whether the solution works. If it doesn't, I refine the\ninstructions until it does.\n3. I check for edge cases.\n4. I do a code review, ask the AI agent to explain what it wrote, and\nhave it make corrections. My own code review is pretty weak, though,\nbecause I haven't spent (and won't spend) thousands of hours working\nwith C and Git internals.\n5. I ask another AI agent to review the code and think through its\nfindings. I usually conclude that its review is better than mine.\n6. I go back to \"writing\" code, and the agent implements the changes\nsuggested during the review.\n7. GOTO 4 if I'm still not satisfied.\n\nAt this point, I face a moral dilemma, because the next step is to\nhand the patch over to other people to read, which means asking them\nto spend their time on it. I have the following options:\n\n1. Report the bug without submitting a patch.\n2. Report the bug and spend a month writing the patch myself, learning\nGit internals and C along the way. Unfortunately, I'm not planning a\ncareer in C. I've been programming for well over a decade in a\ncompletely different stack, and I simply can't afford to devote that\nmuch time to it. It might be worthwhile if I intended to spend the\nnext several years working with Git and C, but unfortunately, that's\nnot an option for me. On top of that, I can't shake the feeling that\nAI is already learning faster than I am.\n3. Report the bug and solve the problem as well as I can with the help\nof an AI agent.\n\nSo realistically, my choice comes down to options 1 and 3. And that's\nmy moral dilemma: I don't know whether it's better not to submit a\npatch at all, or to do the best I can with the time I have, using AI.\n\nChoosing option 3, however, creates another dilemma before submitting\nthe patch: Have I reviewed the code thoroughly enough myself? Do I\nunderstand what I'm doing well enough?\n\nI chose option 3, and out of respect for other people's time, I'm\ndoing my best to understand what I'm submitting.\n\nAnd the only thing I can do to feel that I'm being honest about it is\nto be transparent and explain exactly what my workflow for developing\nthis patch looks like.\n\nThanks,\nMaciej\n\n"},{"id":"554525","messageId":"e64d1300-6c37-4ae4-9377-77dd16f3cf19@app.fastmail.com","threadId":"66350","inReplyTo":"CACQ=SRHRp4vKpV5JagAXS1n4iX-LJ7fm78OwMGB5fke2CTC4FQ@mail.gmail.com","subject":"Re: [PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-10-08T19:19:49Z","receivedAt":"2026-10-08T19:19:49Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Thu, Oct 8, 2026, at 21:06, Maciej Ciemborowicz wrote:\n>[snip]\n> And the only thing I can do to feel that I'm being honest about it is\n> to be transparent and explain exactly what my workflow for developing\n> this patch looks like.\n\nAugment option #1 with stating upfront that one is using a coding agents\nand to what degree. That’s full transparancy and people can then choose\nto engage or not.\n\n"},{"id":"554542","messageId":"CACQ=SRG86pgS+BubOk5Ud6bbeiVOztWT-=KLveJvZt_iKoztRw@mail.gmail.com","threadId":"66350","inReplyTo":"e64d1300-6c37-4ae4-9377-77dd16f3cf19@app.fastmail.com","subject":"Re: [PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-08T21:11:14Z","receivedAt":"2026-10-08T21:11:14Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"On Thu, Oct 8, 2026 at 9:20 PM Kristoffer Haugsbakk\n<kristofferhaugsbakk@fastmail.com> wrote:\n\n> Augment option #1 with stating upfront that one is using a coding agents\n> and to what degree. That’s full transparancy and people can then choose\n> to engage or not.\n\nYes, that could be a rule.\n\nThanks,\nMaciej\n\n"},{"id":"554552","messageId":"ash-bs4-WmGuLyCl@pks.im","threadId":"66350","inReplyTo":"CACQ=SRHRp4vKpV5JagAXS1n4iX-LJ7fm78OwMGB5fke2CTC4FQ@mail.gmail.com","subject":"Re: [PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-10-09T05:41:02Z","receivedAt":"2026-10-09T05:41:02Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Thu, Oct 08, 2026 at 09:06:32PM +0200, Maciej Ciemborowicz wrote:\n> On Thu, Oct 8, 2026 at 5:46 PM Junio C Hamano <gitster@pobox.com> wrote:\n[snip]\n> So realistically, my choice comes down to options 1 and 3. And that's\n> my moral dilemma: I don't know whether it's better not to submit a\n> patch at all, or to do the best I can with the time I have, using AI.\n\nI think one important factor here is the size and complexity of the\nchanges that you are proposing. If the change you're about to submit is\nsmallish and can be reasoned through quite easily then I'd always be in\nfavor of submitting such a patch. But if it's large and complex I'd\nrefrain from doing so because chances are high that there are lots of\nnuances that the AI will get wrong and that you cannot vet because you\nlack the context.\n\nAnd I think that's something that was true even before AI. People that\nwant to contributo to Git have a bit of a ramp-up time, and the guidance\nhas always been to start with microprojects and then work your way up.\nAnd I think that guidance continues to apply even when these newcomers\nare equipped with AI.\n\nPatrick\n\n"},{"id":"554662","messageId":"CAOLa=ZTtLcSPop-A3y-kOv5h-0D-Kq2P+QUpKTEcmU5z77eJYg@mail.gmail.com","threadId":"66350","inReplyTo":"xmqqece02no9.fsf@gitster.g","subject":"Re: [PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-10-09T21:21:40Z","receivedAt":"2026-10-09T21:21:40Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Patrick Steinhardt <ps@pks.im> writes:\n>\n>> On Thu, Oct 08, 2026 at 11:44:15AM +0200, Maciej Ciemborowicz wrote:\n>>> Changes since v3:\n>>>\n>>> * Rebase onto 6de20f6092 (The 4th batch, 2026-10-06), the master commit\n>>>   used in Junio's report.\n>>> * Preserve the packed preparation error in patch 2 as described above.\n>>> * Register t1425 and t1424 in t/meson.build in the commits adding them.\n>>\n>> Please engage with the reviewers. Just posting new versions without\n>> replying to them at all will very likely not get you anywhere. This kind\n>> of behaviour is nowadays a red flag and often hints at contributors who\n>> are basically just a meat proxy. And as a consequence, reviewers are\n>> very likely to disengage and stop reviewing your patch series\n>> altogether, which is frustrating to everyone involved.\n>\n> Thanks for bringing this up.\n>\n> A response to reviews on the N-th round must come long before\n> sending the v(N+1) round of patches.  Some contributors send them\n> after v(N+1), or immediately before, but the proper time to respond\n> is soon after receiving the reviews on vN and having had enough time\n> to understand the comments, before starting work on v(N+1).  Only\n> after that work is complete would you send the new patches.  Hence,\n> we expect the time between vN and v(N+1) from real contributors to\n> be measured in days, not hours.  Whenever I see vN responses arrive\n> after or immediately before the v(N+1) patches, or worse, no\n> response at all but just the new patches, it smells fishy.\n\nThis is kinda why I stopped reviewing the other patches [1] from the\nauthor, my reviews were simply met with N+1 version of the series. At\nsome point I felt it would've been faster if I used a LLM locally for\nthe same task and reviewed its code instead.\n\n[1]: https://lore.kernel.org/git/CACQ=SRGTTdQ+dHXhN6F52dBv5KxZBRfk_Em2fvmEmGJDoB6oTg@mail.gmail.com/\n"},{"id":"554669","messageId":"CACQ=SRHe_gKjr+-TSvo7F+L=vXeV0TwFGRAYJ52bf+09adBFOw@mail.gmail.com","threadId":"66350","inReplyTo":"CAOLa=ZTtLcSPop-A3y-kOv5h-0D-Kq2P+QUpKTEcmU5z77eJYg@mail.gmail.com","subject":"Re: [PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"Maciej Ciemborowicz","fromEmail":"maciej.ciemborowicz@gmail.com","sentAt":"2026-10-10T00:06:50Z","receivedAt":"2026-10-10T00:06:50Z","isPatch":true,"sender":{"key":"maciej.ciemborowicz@gmail.com","avatar":"https://gravatar.com/avatar/a4c9e747f65be11236df62a1a1ef840a828d30911b76b73f1f67b577922dae4e?d=mp&s=160"},"body":"> At some point I felt it would've been faster if I used a LLM locally for\nthe same task and reviewed its code instead.\n\nIf writing the patches on your end would be more efficient than\nreviewing mine, I'd be more than happy to go with that approach.\nUltimately, my main goal is simply to get both issues resolved, the\none discussed here and the one in the thread you referenced. I don't\nwant to hold you back.\n\nThanks,\nMaciej\n\nOn Fri, Oct 9, 2026 at 11:21 PM Karthik Nayak <karthik.188@gmail.com> wrote:\n>\n> Junio C Hamano <gitster@pobox.com> writes:\n>\n> > Patrick Steinhardt <ps@pks.im> writes:\n> >\n> >> On Thu, Oct 08, 2026 at 11:44:15AM +0200, Maciej Ciemborowicz wrote:\n> >>> Changes since v3:\n> >>>\n> >>> * Rebase onto 6de20f6092 (The 4th batch, 2026-10-06), the master commit\n> >>>   used in Junio's report.\n> >>> * Preserve the packed preparation error in patch 2 as described above.\n> >>> * Register t1425 and t1424 in t/meson.build in the commits adding them.\n> >>\n> >> Please engage with the reviewers. Just posting new versions without\n> >> replying to them at all will very likely not get you anywhere. This kind\n> >> of behaviour is nowadays a red flag and often hints at contributors who\n> >> are basically just a meat proxy. And as a consequence, reviewers are\n> >> very likely to disengage and stop reviewing your patch series\n> >> altogether, which is frustrating to everyone involved.\n> >\n> > Thanks for bringing this up.\n> >\n> > A response to reviews on the N-th round must come long before\n> > sending the v(N+1) round of patches.  Some contributors send them\n> > after v(N+1), or immediately before, but the proper time to respond\n> > is soon after receiving the reviews on vN and having had enough time\n> > to understand the comments, before starting work on v(N+1).  Only\n> > after that work is complete would you send the new patches.  Hence,\n> > we expect the time between vN and v(N+1) from real contributors to\n> > be measured in days, not hours.  Whenever I see vN responses arrive\n> > after or immediately before the v(N+1) patches, or worse, no\n> > response at all but just the new patches, it smells fishy.\n>\n> This is kinda why I stopped reviewing the other patches [1] from the\n> author, my reviews were simply met with N+1 version of the series. At\n> some point I felt it would've been faster if I used a LLM locally for\n> the same task and reviewed its code instead.\n>\n> [1]: https://lore.kernel.org/git/CACQ=SRGTTdQ+dHXhN6F52dBv5KxZBRfk_Em2fvmEmGJDoB6oTg@mail.gmail.com/\n\n"},{"id":"554670","messageId":"asmFLjno-vuuN1ZS@fruit.crustytoothpaste.net","threadId":"66350","inReplyTo":"CACQ=SRHRp4vKpV5JagAXS1n4iX-LJ7fm78OwMGB5fke2CTC4FQ@mail.gmail.com","subject":"Re: [PATCH v4 0/4] refs: run copy and rename through transactions","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2026-10-10T00:22:07Z","receivedAt":"2026-10-10T00:22:07Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2026-10-08 at 19:06:32, Maciej Ciemborowicz wrote:\n> At this point, I face a moral dilemma, because the next step is to\n> hand the patch over to other people to read, which means asking them\n> to spend their time on it. I have the following options:\n> \n> 1. Report the bug without submitting a patch.\n> 2. Report the bug and spend a month writing the patch myself, learning\n> Git internals and C along the way. Unfortunately, I'm not planning a\n> career in C. I've been programming for well over a decade in a\n> completely different stack, and I simply can't afford to devote that\n> much time to it. It might be worthwhile if I intended to spend the\n> next several years working with Git and C, but unfortunately, that's\n> not an option for me. On top of that, I can't shake the feeling that\n> AI is already learning faster than I am.\n> 3. Report the bug and solve the problem as well as I can with the help\n> of an AI agent.\n> \n> So realistically, my choice comes down to options 1 and 3. And that's\n> my moral dilemma: I don't know whether it's better not to submit a\n> patch at all, or to do the best I can with the time I have, using AI.\n\nIt's better not to submit a patch at all if you can't submit something\nof reasonably good quality or can't satisfy the guidelines in\nSubmittingPatches (either with regard to AI usage or in any other way).\nI've been contributing for probably a decade and there are lots of times\nI submit a bug report or notice a problem and _don't_ submit a patch,\neither because I'm limited in time or because it's something that I'm\njust not familiar with (like the internals of the pack bitmap code).\nReporting bugs is valuable to us even without a patch and nobody is\nexpected to magically understand all the code.\n\nThe problem you reported has been noticed by other people, so it may be\nthat someone who _is_ familiar with the code or confidence in C picks it\nup.  That happened with a change that went into 2.56 about an issue I\nreported around the 2.51 time frame.  I agree this is something that\nshould ideally be fixed and a fix would be valuable for another problem\nthat I've also reported (`git remote rename` is very expensive with many\nremote-tracking refs), so it's likely someone will pick it up\neventually and that could end up being me if nobody else gets to it\nfirst (no promises, though).\n-- \nbrian m. carlson (they/them)\nToronto, Ontario, CA\n"}]}