Skip to contentSkip to conversation

reference-transaction hook misses destination of git branch -m

BUG Superseded Maciej Ciemborowicz 6 participants 35 messages Sep 19 – Oct 10

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hello,

git branch -m old new changes refs/heads/old into refs/heads/new, but the reference-transaction hook does not report both ref changes.

Observed with Git 2.55:

* files backend: the hook reports deletion of refs/heads/old, but does not report creation of refs/heads/new;
* reftable backend: the hook reports no usable rename payload.

The equivalent atomic operation performed with git update-ref --stdin reports both updates correctly.

I expected the transaction to include:

<oid> <zero> refs/heads/old
<zero> <oid> refs/heads/new

Minimal reproducer:

    #!/bin/sh
    set -eu

    format=${1:-files}
    root=$(mktemp -d)
    trap 'rm -rf "$root"' EXIT

    repo=$root/repo
    hooks=$root/hooks
    log=$root/transactions

    git init -q --ref-format="$format" "$repo"
    git -C "$repo" config user.name Reproducer
    git -C "$repo" config user.email [email protected]
    git -C "$repo" commit --allow-empty -qm initial
    git -C "$repo" branch old

    mkdir "$hooks"
    cat >"$hooks/reference-transaction" <<'HOOK'
    #!/bin/sh
    printf '%s\n' "--- $1" >>"$HOOK_LOG"
    cat >>"$HOOK_LOG"
    HOOK
    chmod +x "$hooks/reference-transaction"

    git -C "$repo" config core.hooksPath "$hooks"
    export HOOK_LOG=$log
    : >"$log"

    git -C "$repo" branch -m old new
    cat "$log"

The behavior was also tested across Git 2.28–2.55:
https://github.com/ciembor/git-hooks-ext/blob/v0.2.0/tests/compat/README.md

The reference-transaction documentation says that the hook is invoked by Git commands performing reference updates. A branch rename changes two refs, but the destination update is not visible to the hook.

Thanks,
Maciej Ciemborowicz

#1 lore raw
Quoted 5 lines · Maciej Ciemborowicz

Hello,

git branch -m old new changes refs/heads/old into refs/heads/new, but the reference-transaction hook does not report both ref changes.

My understanding without digging too deep is that both 'copy' and 'rename' do not use a transaction, as such they don't trigger the 'reference-transaction' hook. This is definitely absurd, since we would expect that all reference operations should use the transaction and trigger the hook.

Quoted 4 lines

Observed with Git 2.55:

* files backend: the hook reports deletion of refs/heads/old, but does not report creation of refs/heads/new;

Yeah, because the files backend calls refs_delete_ref() on the old reference which is done within a transaction.

Quoted 1 line

* reftable backend: the hook reports no usable rename payload.

Reftable doesn't call refs_delete_ref() and simply writes a TOMBSTONE entry to delete the old reference.

Quoted 3 lines

The equivalent atomic operation performed with git update-ref --stdin reports both updates correctly.

By equivalent atomic operation do you a 'delete' and 'create' command?

Quoted 39 lines

I expected the transaction to include:

<oid> <zero> refs/heads/old
<zero> <oid> refs/heads/new

Minimal reproducer:

    #!/bin/sh
    set -eu

    format=${1:-files}
    root=$(mktemp -d)
    trap 'rm -rf "$root"' EXIT

    repo=$root/repo
    hooks=$root/hooks
    log=$root/transactions

    git init -q --ref-format="$format" "$repo"
    git -C "$repo" config user.name Reproducer
    git -C "$repo" config user.email [email protected]
    git -C "$repo" commit --allow-empty -qm initial
    git -C "$repo" branch old

    mkdir "$hooks"
    cat >"$hooks/reference-transaction" <<'HOOK'
    #!/bin/sh
    printf '%s\n' "--- $1" >>"$HOOK_LOG"
    cat >>"$HOOK_LOG"
    HOOK
    chmod +x "$hooks/reference-transaction"

    git -C "$repo" config core.hooksPath "$hooks"
    export HOOK_LOG=$log
    : >"$log"

    git -C "$repo" branch -m old new
    cat "$log"
Quoted 7 lines

The behavior was also tested across Git 2.28–2.55:
https://github.com/ciembor/git-hooks-ext/blob/v0.2.0/tests/compat/README.md

The reference-transaction documentation says that the hook is invoked by Git commands performing reference updates. A branch rename changes two refs, but the destination update is not visible to the hook.

Either ways, I think we should fix this, I could have a look if you aren't submitting a patch yourself :)

Quoted 2 lines

Thanks,
Maciej Ciemborowicz

#2 lore raw

PATCH 1/1 refs: run copy and rename through transactions

Reference copy and rename operations currently bypass the transaction API. Consequently, the reference-transaction hook sees only the source deletion with the files backend and no useful update with the reftable backend.

Represent both operations as reference transactions containing their logical updates. A rename is a deletion of the old reference and creation of the new reference in the same transaction. Retain backend-specific reflog handling: the files backend stages its existing rename procedure across prepare, finish and abort, while reftable stages an addition while holding the stack lock. Suppress hooks for the files backend's nested deletion transactions so that callers observe one logical transaction.

Record and verify the source and destination values after taking backend locks. This rejects concurrent changes instead of applying a rename or copy that differs from the payload shown to the preparing hook. Preserve D/F renames and restore overwritten references and reflogs when a prepared hook rejects the operation.

Add coverage for rename, copy, forced updates, both directions of D/F conflicts, concurrent updates and prepared-hook rollback.

---
 refs.c                           | 137 ++++++++++++---
 refs.h                           |   3 +
 refs/debug.c                     |  25 ---
 refs/files-backend.c             | 276 +++++++++++++++++++++++++++----
 refs/packed-backend.c            |   2 -
 refs/refs-internal.h             |  38 +++--
 refs/reftable-backend.c          | 194 ++++++++++++++++------
 t/t1416-ref-transaction-hooks.sh | 142 ++++++++++++++++
 8 files changed, 679 insertions(+), 138 deletions(-)
refs.c +114 -23
index 92d5df5b7..22c000f7f 100644
@@ -1004,15 +1004,17 @@ long get_files_ref_lock_timeout_ms(struct repository *repo)
 	return timeout_ms;
 }
 
-int refs_delete_ref(struct ref_store *refs, const char *msg,
-		    const char *refname,
-		    const struct object_id *old_oid,
-		    unsigned int flags)
+int refs_delete_ref_with_transaction_flags(struct ref_store *refs,
+					   const char *msg,
+					   const char *refname,
+					   const struct object_id *old_oid,
+					   unsigned int flags,
+					   unsigned int transaction_flags)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
 
-	transaction = ref_store_transaction_begin(refs, 0, &err);
+	transaction = ref_store_transaction_begin(refs, transaction_flags, &err);
 	if (!transaction ||
 	    ref_transaction_delete(transaction, refname, old_oid,
 				   NULL, flags, msg, &err) ||
@@ -1027,6 +1029,15 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
 	return 0;
 }
 
+int refs_delete_ref(struct ref_store *refs, const char *msg,
+		    const char *refname,
+		    const struct object_id *old_oid,
+		    unsigned int flags)
+{
+	return refs_delete_ref_with_transaction_flags(refs, msg, refname,
+						      old_oid, flags, 0);
+}
+
 static void copy_reflog_msg(struct strbuf *sb, const char *msg)
 {
 	char c;
@@ -1270,6 +1281,10 @@ void ref_transaction_free(struct ref_transaction *transaction)
 
 	string_list_clear(&transaction->refnames, 0);
 	free(transaction->updates);
+	free(transaction->old_refname);
+	free(transaction->new_refname);
+	free(transaction->logmsg);
+	free(transaction->destination_target);
 	free(transaction);
 }
 
@@ -2710,7 +2725,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,
 		return REF_TRANSACTION_ERROR_GENERIC;
 
 	/* Preparing checks before locking references */
-	ret = run_transaction_hook(transaction, "preparing");
+	ret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :
+		run_transaction_hook(transaction, "preparing");
 	if (ret) {
 		ref_transaction_abort(transaction, err);
 		die(_(abort_by_ref_transaction_hook), "preparing");
@@ -2720,7 +2736,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,
 	if (ret)
 		return ret;
 
-	ret = run_transaction_hook(transaction, "prepared");
+	ret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :
+		run_transaction_hook(transaction, "prepared");
 	if (ret) {
 		ref_transaction_abort(transaction, err);
 		die(_(abort_by_ref_transaction_hook), "prepared");
@@ -2750,7 +2767,8 @@ int ref_transaction_abort(struct ref_transaction *transaction,
 		break;
 	}
 
-	run_transaction_hook(transaction, "aborted");
+	if (!(transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK))
+		run_transaction_hook(transaction, "aborted");
 
 	ref_transaction_free(transaction);
 	return ret;
@@ -2781,7 +2799,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 	}
 
 	ret = refs->be->transaction_finish(refs, transaction, err);
-	if (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL))
+	if (!ret && !(transaction->flags & (REF_TRANSACTION_FLAG_INITIAL |
+					 REF_TRANSACTION_FLAG_SKIP_HOOK)))
 		run_transaction_hook(transaction, "committed");
 	return ret;
 }
@@ -3123,28 +3142,100 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,
 	return ret;
 }
 
-int refs_rename_ref(struct ref_store *refs, const char *oldref,
-		    const char *newref, const char *logmsg)
+static int refs_copy_or_rename_ref(struct ref_store *refs, const char *oldref,
+				   const char *newref, const char *logmsg,
+				   int copy)
 {
-	char *msg;
-	int retval;
+	struct ref_transaction *transaction = NULL;
+	struct object_id old_oid, new_oid;
+	struct strbuf new_target = STRBUF_INIT;
+	struct strbuf err = STRBUF_INIT;
+	char *msg = normalize_reflog_message(logmsg);
+	int old_flags, new_flags = 0, new_exists = 0, ret = 1;
 
-	msg = normalize_reflog_message(logmsg);
-	retval = refs->be->rename_ref(refs, oldref, newref, msg);
+	if (!strcmp(oldref, newref)) {
+		ret = 0;
+		goto out;
+	}
+
+	if (!refs_resolve_ref_unsafe(refs, oldref,
+				     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
+				     &old_oid, &old_flags)) {
+		error("refname %s not found", oldref);
+		goto out;
+	}
+	if (old_flags & REF_ISSYMREF) {
+		error("refname %s is a symbolic ref, %s it is not supported",
+		      oldref, copy ? "copying" : "renaming");
+		goto out;
+	}
+
+	transaction = ref_store_transaction_begin(refs, 0, &err);
+	if (!transaction)
+		goto error;
+	transaction->type = copy ? REF_TRANSACTION_TYPE_COPY :
+		REF_TRANSACTION_TYPE_RENAME;
+	transaction->old_refname = xstrdup(oldref);
+	transaction->new_refname = xstrdup(newref);
+	transaction->logmsg = xstrdup(msg);
+	oidcpy(&transaction->source_oid, &old_oid);
+
+	if (!copy && ref_transaction_delete(transaction, oldref, &old_oid, NULL,
+					    REF_NO_DEREF, msg, &err))
+		goto error;
+
+	if (refs_resolve_ref_unsafe(refs, newref,
+				    RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
+				    &new_oid, &new_flags)) {
+		new_exists = 1;
+		if ((new_flags & REF_ISSYMREF) &&
+		    refs_read_symbolic_ref(refs, newref, &new_target) < 0) {
+			strbuf_addf(&err, "unable to read symbolic ref %s", newref);
+			goto error;
+		}
+	} else {
+		oidclr(&new_oid, refs->repo->hash_algo);
+	}
+	transaction->destination_exists = new_exists;
+	if (new_flags & REF_ISSYMREF)
+		transaction->destination_target = xstrdup(new_target.buf);
+	else if (transaction->destination_exists)
+		oidcpy(&transaction->destination_oid, &new_oid);
+
+	if (ref_transaction_update(transaction, newref, &old_oid,
+				   (new_flags & REF_ISSYMREF) ? NULL : &new_oid,
+				   NULL,
+				   (new_flags & REF_ISSYMREF) ? new_target.buf : NULL,
+				   REF_NO_DEREF | REF_SKIP_CREATE_REFLOG,
+				   NULL, &err))
+		goto error;
+
+	if (ref_transaction_commit(transaction, &err))
+		goto error;
+
+	ret = 0;
+	goto out;
+
+error:
+	error("%s", err.buf);
+out:
+	ref_transaction_free(transaction);
+	strbuf_release(&new_target);
+	strbuf_release(&err);
 	free(msg);
-	return retval;
+	return ret;
 }
 
-int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
+int refs_rename_ref(struct ref_store *refs, const char *oldref,
 		    const char *newref, const char *logmsg)
 {
-	char *msg;
-	int retval;
+	return refs_copy_or_rename_ref(refs, oldref, newref, logmsg, 0);
+}
 
-	msg = normalize_reflog_message(logmsg);
-	retval = refs->be->copy_ref(refs, oldref, newref, msg);
-	free(msg);
-	return retval;
+int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
+		    const char *newref, const char *logmsg)
+{
+	return refs_copy_or_rename_ref(refs, oldref, newref, logmsg, 1);
 }
 
 const char *ref_update_original_update_refname(struct ref_update *update)
refs.h +3 -0
index 9979446d1..25bb8720f 100644
@@ -784,6 +784,9 @@ enum ref_transaction_flag {
 	 * while rejecting updates which do not match the expected state.
 	 */
 	REF_TRANSACTION_ALLOW_FAILURE = (1 << 1),
+
+	/* Suppress hooks for an update nested in another transaction. */
+	REF_TRANSACTION_FLAG_SKIP_HOOK = (1 << 2),
 };
 
 /*
refs/debug.c +0 -25
index 639db0f26..87b84e767 100644
@@ -143,28 +143,6 @@ static int debug_optimize_required(struct ref_store *ref_store,
 	return res;
 }
 
-static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,
-			    const char *newref, const char *logmsg)
-{
-	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref,
-					      logmsg);
-	trace_printf_key(&trace_refs, "rename_ref: %s -> %s \"%s\": %d\n", oldref, newref,
-		logmsg, res);
-	return res;
-}
-
-static int debug_copy_ref(struct ref_store *ref_store, const char *oldref,
-			  const char *newref, const char *logmsg)
-{
-	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res =
-		drefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg);
-	trace_printf_key(&trace_refs, "copy_ref: %s -> %s \"%s\": %d\n", oldref, newref,
-		logmsg, res);
-	return res;
-}
-
 struct debug_ref_iterator {
 	struct ref_iterator base;
 	struct ref_iterator *iter;
@@ -453,9 +431,6 @@ struct ref_storage_be refs_be_debug = {
 	.optimize = debug_optimize,
 	.optimize_required = debug_optimize_required,
 
-	.rename_ref = debug_rename_ref,
-	.copy_ref = debug_copy_ref,
-
 	.iterator_begin = debug_ref_iterator_begin,
 	.read_raw_ref = debug_read_raw_ref,
 	.read_symbolic_ref = debug_read_symbolic_ref,
refs/files-backend.c +246 -30

391 lines not on this page · raw message

refs/packed-backend.c +0 -2
index a73fc6aca..364a91291 100644
@@ -2164,8 +2164,6 @@ struct ref_storage_be refs_be_packed = {
 	.optimize = packed_optimize,
 	.optimize_required = packed_optimize_required,
 
-	.rename_ref = NULL,
-	.copy_ref = NULL,
 
 	.iterator_begin = packed_ref_iterator_begin,
 	.read_raw_ref = packed_read_raw_ref,
refs/refs-internal.h +28 -10
index c3ac7b556..406b54b65 100644
@@ -187,6 +187,13 @@ struct ref_update *ref_transaction_add_update(
 		const char *committer_info,
 		const char *msg);
 
+int refs_delete_ref_with_transaction_flags(struct ref_store *refs,
+					   const char *msg,
+					   const char *refname,
+					   const struct object_id *old_oid,
+					   unsigned int flags,
+					   unsigned int transaction_flags);
+
 /*
  * Transaction states.
  *
@@ -212,6 +219,12 @@ enum ref_transaction_state {
 	REF_TRANSACTION_CLOSED   = 2
 };
 
+enum ref_transaction_type {
+	REF_TRANSACTION_TYPE_NORMAL = 0,
+	REF_TRANSACTION_TYPE_RENAME,
+	REF_TRANSACTION_TYPE_COPY,
+};
+
 /*
  * Data structure to hold indices of updates which were rejected, for batched
  * reference updates. While the updates themselves hold the rejection error,
@@ -240,6 +253,21 @@ struct ref_transaction {
 	void *backend_data;
 	unsigned int flags;
 	uint64_t max_index;
+
+	/*
+	 * Rename and copy operations need backend-specific reflog handling.
+	 * Their logical updates still live in `updates`, so hooks see the
+	 * operation like any other reference transaction. The fields below
+	 * retain the state that backends verify after taking their locks.
+	 */
+	enum ref_transaction_type type;
+	char *old_refname;
+	char *new_refname;
+	char *logmsg;
+	struct object_id source_oid;
+	struct object_id destination_oid;
+	char *destination_target;
+	unsigned int destination_exists:1;
 };
 
 /*
@@ -451,13 +479,6 @@ typedef int optimize_required_fn(struct ref_store *ref_store,
 				 struct refs_optimize_opts *opts,
 				 bool *required);
 
-typedef int rename_ref_fn(struct ref_store *ref_store,
-			  const char *oldref, const char *newref,
-			  const char *logmsg);
-typedef int copy_ref_fn(struct ref_store *ref_store,
-			  const char *oldref, const char *newref,
-			  const char *logmsg);
-
 /*
  * Iterate over the references in `ref_store` whose names start with
  * `prefix`. `prefix` is matched as a literal string, without regard
@@ -577,9 +598,6 @@ struct ref_storage_be {
 
 	optimize_fn *optimize;
 	optimize_required_fn *optimize_required;
-	rename_ref_fn *rename_ref;
-	copy_ref_fn *copy_ref;
-
 	ref_iterator_begin_fn *iterator_begin;
 	read_raw_ref_fn *read_raw_ref;
 
refs/reftable-backend.c +146 -48

288 lines not on this page · raw message

t/t1416-ref-transaction-hooks.sh +142 -0

148 lines not on this page · raw message

base-commit: d38352cd43ab9745686d697872408bc3249a153f

Signature · 1 line
2.39.3 (Apple Git-146)
#3 lore raw

Re: [PATCH] refs: run copy and rename through transactions

Quoted 18 lines · Maciej Ciemborowicz

Reference copy and rename operations currently bypass the transaction API. Consequently, the reference-transaction hook sees only the source deletion with the files backend and no useful update with the reftable backend.

Represent both operations as reference transactions containing their logical updates. A rename is a deletion of the old reference and creation of the new reference in the same transaction. Retain backend-specific reflog handling: the files backend stages its existing rename procedure across prepare, finish and abort, while reftable stages an addition while holding the stack lock. Suppress hooks for the files backend's nested deletion transactions so that callers observe one logical transaction.

Record and verify the source and destination values after taking backend locks. This rejects concurrent changes instead of applying a rename or copy that differs from the payload shown to the preparing hook. Preserve D/F renames and restore overwritten references and reflogs when a prepared hook rejects the operation.

Quoted 6 lines

Add coverage for rename, copy, forced updates, both directions of D/F conflicts, concurrent updates and prepared-hook rollback.

---

Drop unnecessary "currently" to the first sentence, and add "test" to the laste sentence somewhere, and this would be perfect.

Very pleasing to see an exceptionally well-written proposed commit log message by a new contributor.

Quoted 105 lines
 refs.c                           | 137 ++++++++++++---
 refs.h                           |   3 +
 refs/debug.c                     |  25 ---
 refs/files-backend.c             | 276 +++++++++++++++++++++++++++----
 refs/packed-backend.c            |   2 -
 refs/refs-internal.h             |  38 +++--
 refs/reftable-backend.c          | 194 ++++++++++++++++------
 t/t1416-ref-transaction-hooks.sh | 142 ++++++++++++++++
 8 files changed, 679 insertions(+), 138 deletions(-)
refs.c +28 -9
index 92d5df5b7..22c000f7f 100644
@@ -1004,15 +1004,17 @@ long get_files_ref_lock_timeout_ms(struct repository *repo)
 	return timeout_ms;
 }
 
-int refs_delete_ref(struct ref_store *refs, const char *msg,
-		    const char *refname,
-		    const struct object_id *old_oid,
-		    unsigned int flags)
+int refs_delete_ref_with_transaction_flags(struct ref_store *refs,
+					   const char *msg,
+					   const char *refname,
+					   const struct object_id *old_oid,
+					   unsigned int flags,
+					   unsigned int transaction_flags)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
 
-	transaction = ref_store_transaction_begin(refs, 0, &err);
+	transaction = ref_store_transaction_begin(refs, transaction_flags, &err);
 	if (!transaction ||
 	    ref_transaction_delete(transaction, refname, old_oid,
 				   NULL, flags, msg, &err) ||
@@ -1027,6 +1029,15 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
 	return 0;
 }
 
+int refs_delete_ref(struct ref_store *refs, const char *msg,
+		    const char *refname,
+		    const struct object_id *old_oid,
+		    unsigned int flags)
+{
+	return refs_delete_ref_with_transaction_flags(refs, msg, refname,
+						      old_oid, flags, 0);
+}
+
 static void copy_reflog_msg(struct strbuf *sb, const char *msg)
 {
 	char c;
@@ -1270,6 +1281,10 @@ void ref_transaction_free(struct ref_transaction *transaction)
 
 	string_list_clear(&transaction->refnames, 0);
 	free(transaction->updates);
+	free(transaction->old_refname);
+	free(transaction->new_refname);
+	free(transaction->logmsg);
+	free(transaction->destination_target);
 	free(transaction);
 }
 
@@ -2710,7 +2725,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,
 		return REF_TRANSACTION_ERROR_GENERIC;
 
 	/* Preparing checks before locking references */
-	ret = run_transaction_hook(transaction, "preparing");
+	ret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :
+		run_transaction_hook(transaction, "preparing");
 	if (ret) {
 		ref_transaction_abort(transaction, err);
 		die(_(abort_by_ref_transaction_hook), "preparing");
@@ -2720,7 +2736,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,
 	if (ret)
 		return ret;
 
-	ret = run_transaction_hook(transaction, "prepared");
+	ret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :
+		run_transaction_hook(transaction, "prepared");
 	if (ret) {
 		ref_transaction_abort(transaction, err);
 		die(_(abort_by_ref_transaction_hook), "prepared");
@@ -2750,7 +2767,8 @@ int ref_transaction_abort(struct ref_transaction *transaction,
 		break;
 	}
 
-	run_transaction_hook(transaction, "aborted");
+	if (!(transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK))
+		run_transaction_hook(transaction, "aborted");
 
 	ref_transaction_free(transaction);
 	return ret;
@@ -2781,7 +2799,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 	}
 
 	ret = refs->be->transaction_finish(refs, transaction, err);
-	if (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL))
+	if (!ret && !(transaction->flags & (REF_TRANSACTION_FLAG_INITIAL |
+					 REF_TRANSACTION_FLAG_SKIP_HOOK)))
 		run_transaction_hook(transaction, "committed");
 	return ret;
 }

refs.c int refs_delete_refs(struct ref_store *refs, const char *logmsg, · line 3127

Quoted 5 lines
refs.c +0 -2
@@ -3123,28 +3142,100 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,
 	return ret;
 }
 
-int refs_rename_ref(struct ref_store *refs, const char *oldref,
-		    const char *newref, const char *logmsg)

It is annoying that we have to give random callers an unrestricted way to skip calling hooks. I suspect it may come from "this function should call hook when invoked as the top-level operation, but when it is used as a subroutine for a different top-level operation, we want to skip hooks" kind of reasoning, but is this something we can avoid by rearranging the call chain?

refs.c int refs_delete_refs(struct ref_store *refs, const char *logmsg, · line 3147

Quoted 3 lines
refs.c +3 -0
+static int refs_copy_or_rename_ref(struct ref_store *refs, const char *oldref,
+				   const char *newref, const char *logmsg,
+				   int copy)

Will this function ever gain a third mode of operation other than copy or rename? If not, perhaps "bool copy"?

Quoted 75 lines
refs.c +69 -4
 {
-	char *msg;
-	int retval;
+	struct ref_transaction *transaction = NULL;
+	struct object_id old_oid, new_oid;
+	struct strbuf new_target = STRBUF_INIT;
+	struct strbuf err = STRBUF_INIT;
+	char *msg = normalize_reflog_message(logmsg);
+	int old_flags, new_flags = 0, new_exists = 0, ret = 1;
 
-	msg = normalize_reflog_message(logmsg);
-	retval = refs->be->rename_ref(refs, oldref, newref, msg);
+	if (!strcmp(oldref, newref)) {
+		ret = 0;
+		goto out;
+	}
+
+	if (!refs_resolve_ref_unsafe(refs, oldref,
+				     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
+				     &old_oid, &old_flags)) {
+		error("refname %s not found", oldref);
+		goto out;
+	}
+	if (old_flags & REF_ISSYMREF) {
+		error("refname %s is a symbolic ref, %s it is not supported",
+		      oldref, copy ? "copying" : "renaming");
+		goto out;
+	}
+
+	transaction = ref_store_transaction_begin(refs, 0, &err);
+	if (!transaction)
+		goto error;
+	transaction->type = copy ? REF_TRANSACTION_TYPE_COPY :
+		REF_TRANSACTION_TYPE_RENAME;
+	transaction->old_refname = xstrdup(oldref);
+	transaction->new_refname = xstrdup(newref);
+	transaction->logmsg = xstrdup(msg);
+	oidcpy(&transaction->source_oid, &old_oid);
+
+	if (!copy && ref_transaction_delete(transaction, oldref, &old_oid, NULL,
+					    REF_NO_DEREF, msg, &err))
+		goto error;
+
+	if (refs_resolve_ref_unsafe(refs, newref,
+				    RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
+				    &new_oid, &new_flags)) {
+		new_exists = 1;
+		if ((new_flags & REF_ISSYMREF) &&
+		    refs_read_symbolic_ref(refs, newref, &new_target) < 0) {
+			strbuf_addf(&err, "unable to read symbolic ref %s", newref);
+			goto error;
+		}
+	} else {
+		oidclr(&new_oid, refs->repo->hash_algo);
+	}
+	transaction->destination_exists = new_exists;
+	if (new_flags & REF_ISSYMREF)
+		transaction->destination_target = xstrdup(new_target.buf);
+	else if (transaction->destination_exists)
+		oidcpy(&transaction->destination_oid, &new_oid);
+
+	if (ref_transaction_update(transaction, newref, &old_oid,
+				   (new_flags & REF_ISSYMREF) ? NULL : &new_oid,
+				   NULL,
+				   (new_flags & REF_ISSYMREF) ? new_target.buf : NULL,
+				   REF_NO_DEREF | REF_SKIP_CREATE_REFLOG,
+				   NULL, &err))
+		goto error;
+
+	if (ref_transaction_commit(transaction, &err))
+		goto error;
+
+	ret = 0;
+	goto out;
+

refs.c int refs_delete_refs(struct ref_store *refs, const char *logmsg, · line 3227

Quoted 10 lines
refs.c +7 -1
+error:
+	error("%s", err.buf);
+out:
+	ref_transaction_free(transaction);
+	strbuf_release(&new_target);
+	strbuf_release(&err);
 	free(msg);
-	return retval;
+	return ret;
 }

That's quite a lot of new code. I see ref_transaction_delete(), ref_transaction_update() and others are already reused from existing code paths, which is good.

refs.c int refs_delete_refs(struct ref_store *refs, const char *logmsg, · line 3236

Quoted 9 lines
refs.c +9 -0
+struct files_copy_or_rename_transaction_data {
+	struct ref_lock *lock;
+	struct object_id orig_oid;
+	struct object_id destination_oid;
+	char *destination_target;
+	int logmoved;
+	int destination_exists;
+	int destination_log_backed_up;
+};

Good to have a type that can be used to hold pieces of information specific to the operation. Can't we do without rename/copy specific addition to the generic ref_transaction struct by following the same principle?

The comment above the members does make it understandable, but ...

Quoted 10 lines
refs.c +6 -0
@@ -240,6 +253,21 @@ struct ref_transaction {
 	void *backend_data;
 	unsigned int flags;
 	uint64_t max_index;
+
+	/*
+	 * Rename and copy operations need backend-specific reflog handling.
+	 * Their logical updates still live in `updates`, so hooks see the
+	 * operation like any other reference transaction. The fields below
+	 * retain the state that backends verify after taking their locks.

refs.c struct ref_transaction { · line 271

Quoted 10 lines
refs.c +9 -0
+	 */
+	enum ref_transaction_type type;
+	char *old_refname;
+	char *new_refname;
+	char *logmsg;
+	struct object_id source_oid;
+	struct object_id destination_oid;
+	char *destination_target;
+	unsigned int destination_exists:1;
 };

... is it the best we can do to contaminate a rather generic data structure for such a details relevant only to one specific operation?

Thanks.

#4 lore raw

Re: [PATCH] refs: run copy and rename through transactions

Quoted 33 lines · Junio C Hamano

refs/files-backend.c static int refs_rename_ref_available(struct ref_store *refs, · line 1697

Quoted 9 lines
refs/files-backend.c +9 -0
+struct files_copy_or_rename_transaction_data {
+	struct ref_lock *lock;
+	struct object_id orig_oid;
+	struct object_id destination_oid;
+	char *destination_target;
+	int logmoved;
+	int destination_exists;
+	int destination_log_backed_up;
+};

Good to have a type that can be used to hold pieces of information specific to the operation. Can't we do without rename/copy specific addition to the generic ref_transaction struct by following the same principle?

The comment above the members does make it understandable, but ...

Quoted 16 lines
refs/refs-internal.h +12 -0
@@ -240,6 +253,21 @@ struct ref_transaction {
 	void *backend_data;
 	unsigned int flags;
 	uint64_t max_index;
+
+	/*
+	 * Rename and copy operations need backend-specific reflog handling.
+	 * Their logical updates still live in `updates`, so hooks see the
+	 * operation like any other reference transaction. The fields below
+	 * retain the state that backends verify after taking their locks.
+	 */
+	enum ref_transaction_type type;
+	char *old_refname;
+	char *new_refname;
+	char *logmsg;
+	struct object_id source_oid;
Quoted 8 lines

refs/refs-internal.h struct ref_transaction { · line 271

Quoted 4 lines
refs/refs-internal.h +3 -0
+	struct object_id destination_oid;
+	char *destination_target;
+	unsigned int destination_exists:1;
 };

... is it the best we can do to contaminate a rather generic data structure for such a details relevant only to one specific operation?

More importantly, this structure suggests to me that you can have a single rename (or copy) from one source to one destination in a single transaction. Is that correct or am I misunderstanding the way this data structure is used? How would one rename A, B and C to X, Y and Z in a single transaction? Or perhaps rename A to B and copy C to D in a single transaction?

#5 lore raw

Re: [PATCH] refs: run copy and rename through transactions

Quoted 6 lines · Junio C Hamano

More importantly, this structure suggests to me that you can have a single rename (or copy) from one source to one destination in a single transaction. Is that correct or am I misunderstanding the way this data structure is used? How would one rename A, B and C to X, Y and Z in a single transaction? Or perhaps rename A to B and copy C to D in a single transaction?

Good point. The current design indeed makes copy/rename a property of the whole transacton, so it does not compose with multiple such operations. I'll rework it so that state belongs to individual updates instead. Unfortunately, I'll have to put this aside for a few days until I finish this first:
https://lore.kernel.org/git/CACQ=SRHthWOLVXmY6wgknOPgpQ+oB1vV-Q0AL=mK9mXb2Xy9Nw@mail.gmail.com/T/#t

Thanks,
Maciej Ciemborowicz

#6 lore raw

PATCH v2 1/1 refs: run copy and rename through transactions

In reply to Maciej Ciemborowicz

Reference copy and rename operations bypass the transaction API. Consequently, the reference-transaction hook sees only the source deletion with the files backend and no useful update with the reftable backend.

Represent both operations as reference transactions containing their logical updates. A rename is a deletion of the old reference and creation of the new reference in the same transaction. Attach operation-specific state to the destination update instead of making copy or rename a property of the entire transaction.

Retain backend-specific reflog handling: the files backend stages its existing rename procedure across prepare, finish and abort, while reftable stages an addition while holding the stack lock. Suppress hooks for the files backend's nested deletion transactions so that callers observe one logical transaction.

Record and verify the source and destination values after taking backend locks. This rejects concurrent changes instead of applying a rename or copy that differs from the payload shown to the preparing hook. Preserve D/F renames and restore overwritten references and reflogs when a prepared hook rejects the operation.

Add tests covering rename, copy, forced updates, both directions of D/F conflicts, concurrent updates and prepared-hook rollback.

---
Apologies for the unrelated Subject header on my earlier reply. This reroll incorporates the points discussed there.

Changes since v1:

* Keep copy/rename state on the destination ref update instead of the generic transaction, so the operation is no longer a transaction-wide property.
* Keep REF_TRANSACTION_FLAG_SKIP_HOOK private to the refs implementation.
* Use bool for the two-state copy parameter.
* Apply Junio's commit-message wording suggestions.

Range-diff against v1:
1: de0a5a9f7 ! 1: d852537d8 refs: run copy and rename through transactions

    @@ Metadata
      ## Commit message ##
         refs: run copy and rename through transactions
     
    -    Reference copy and rename operations currently bypass the transaction API.
    +    Reference copy and rename operations bypass the transaction API.
         Consequently, the reference-transaction hook sees only the source deletion
         with the files backend and no useful update with the reftable backend.
     
         Represent both operations as reference transactions containing their
         logical updates. A rename is a deletion of the old reference and creation
    -    of the new reference in the same transaction. Retain backend-specific
    -    reflog handling: the files backend stages its existing rename procedure
    -    across prepare, finish and abort, while reftable stages an addition while
    -    holding the stack lock. Suppress hooks for the files backend's nested
    -    deletion transactions so that callers observe one logical transaction.
    +    of the new reference in the same transaction. Attach operation-specific
    +    state to the destination update instead of making copy or rename a property
    +    of the entire transaction.
    +
    +    Retain backend-specific reflog handling: the files backend stages its
    +    existing rename procedure across prepare, finish and abort, while reftable
    +    stages an addition while holding the stack lock. Suppress hooks for the
    +    files backend's nested deletion transactions so that callers observe one
    +    logical transaction.
     
         Record and verify the source and destination values after taking backend
         locks. This rejects concurrent changes instead of applying a rename or copy
    @@ Commit message
         renames and restore overwritten references and reflogs when a prepared hook
         rejects the operation.
     
    -    Add coverage for rename, copy, forced updates, both directions of D/F
    +    Add tests covering rename, copy, forced updates, both directions of D/F
         conflicts, concurrent updates and prepared-hook rollback.
     
         Helped-by: Karthik Nayak <[email protected]>
    @@ refs.c: int refs_delete_ref(struct ref_store *refs, const char *msg,
      {
      	char c;
     @@ refs.c: void ref_transaction_free(struct ref_transaction *transaction)
    + 	}
    + 
    + 	for (i = 0; i < transaction->nr; i++) {
    ++		struct ref_copy_or_rename_update *operation =
    ++			transaction->updates[i]->copy_or_rename;
    ++
    + 		free(transaction->updates[i]->msg);
    + 		free(transaction->updates[i]->committer_info);
    + 		free((char *)transaction->updates[i]->new_target);
    + 		free((char *)transaction->updates[i]->old_target);
    + 		free((char *)transaction->updates[i]->rejection_details);
    ++		if (operation) {
    ++			free(operation->old_refname);
    ++			free(operation->logmsg);
    ++			free(operation->destination_target);
    ++			free(operation);
    ++		}
    + 		free(transaction->updates[i]);
    + 	}
      
    - 	string_list_clear(&transaction->refnames, 0);
    - 	free(transaction->updates);
    -+	free(transaction->old_refname);
    -+	free(transaction->new_refname);
    -+	free(transaction->logmsg);
    -+	free(transaction->destination_target);
    +@@ refs.c: void ref_transaction_free(struct ref_transaction *transaction)
      	free(transaction);
      }
      
    ++struct ref_update *ref_transaction_copy_or_rename_update(
    ++	struct ref_transaction *transaction)
    ++{
    ++	struct ref_update *operation = NULL;
    ++	size_t i;
    ++
    ++	for (i = 0; i < transaction->nr; i++) {
    ++		if (!transaction->updates[i]->copy_or_rename)
    ++			continue;
    ++		if (operation)
    ++			BUG("multiple copy or rename updates in one transaction");
    ++		operation = transaction->updates[i];
    ++	}
    ++
    ++	return operation;
    ++}
    ++
    + int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,
    + 				       size_t update_idx,
    + 				       enum ref_transaction_error err,
     @@ refs.c: int ref_transaction_prepare(struct ref_transaction *transaction,
      		return REF_TRANSACTION_ERROR_GENERIC;
      
    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,
     -		    const char *newref, const char *logmsg)
     +static int refs_copy_or_rename_ref(struct ref_store *refs, const char *oldref,
     +				   const char *newref, const char *logmsg,
    -+				   int copy)
    ++				   bool copy)
      {
     -	char *msg;
     -	int retval;
     +	struct ref_transaction *transaction = NULL;
    ++	struct ref_copy_or_rename_update *operation = NULL;
    ++	struct ref_update *destination_update;
     +	struct object_id old_oid, new_oid;
     +	struct strbuf new_target = STRBUF_INIT;
     +	struct strbuf err = STRBUF_INIT;
    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,
     +	transaction = ref_store_transaction_begin(refs, 0, &err);
     +	if (!transaction)
     +		goto error;
    -+	transaction->type = copy ? REF_TRANSACTION_TYPE_COPY :
    -+		REF_TRANSACTION_TYPE_RENAME;
    -+	transaction->old_refname = xstrdup(oldref);
    -+	transaction->new_refname = xstrdup(newref);
    -+	transaction->logmsg = xstrdup(msg);
    -+	oidcpy(&transaction->source_oid, &old_oid);
    -+
     +	if (!copy && ref_transaction_delete(transaction, oldref, &old_oid, NULL,
     +					    REF_NO_DEREF, msg, &err))
     +		goto error;
    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,
     +	} else {
     +		oidclr(&new_oid, refs->repo->hash_algo);
     +	}
    -+	transaction->destination_exists = new_exists;
    -+	if (new_flags & REF_ISSYMREF)
    -+		transaction->destination_target = xstrdup(new_target.buf);
    -+	else if (transaction->destination_exists)
    -+		oidcpy(&transaction->destination_oid, &new_oid);
    -+
     +	if (ref_transaction_update(transaction, newref, &old_oid,
     +				   (new_flags & REF_ISSYMREF) ? NULL : &new_oid,
     +				   NULL,
    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,
     +				   NULL, &err))
     +		goto error;
     +
    ++	destination_update = transaction->updates[transaction->nr - 1];
    ++	CALLOC_ARRAY(operation, 1);
    ++	operation->type = copy ? REF_UPDATE_COPY : REF_UPDATE_RENAME;
    ++	operation->old_refname = xstrdup(oldref);
    ++	operation->logmsg = xstrdup(msg);
    ++	oidcpy(&operation->source_oid, &old_oid);
    ++	operation->destination_exists = new_exists;
    ++	if (new_flags & REF_ISSYMREF)
    ++		operation->destination_target = xstrdup(new_target.buf);
    ++	else if (operation->destination_exists)
    ++		oidcpy(&operation->destination_oid, &new_oid);
    ++	destination_update->copy_or_rename = operation;
    ++
     +	if (ref_transaction_commit(transaction, &err))
     +		goto error;
     +
    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,
      
      const char *ref_update_original_update_refname(struct ref_update *update)
     
    - ## refs.h ##
    -@@ refs.h: enum ref_transaction_flag {
    - 	 * while rejecting updates which do not match the expected state.
    - 	 */
    - 	REF_TRANSACTION_ALLOW_FAILURE = (1 << 1),
    -+
    -+	/* Suppress hooks for an update nested in another transaction. */
    -+	REF_TRANSACTION_FLAG_SKIP_HOOK = (1 << 2),
    - };
    - 
    - /*
    -
      ## refs/debug.c ##
     @@ refs/debug.c: static int debug_optimize_required(struct ref_store *ref_store,
      	return res;
    @@ refs/files-backend.c: static int refs_rename_ref_available(struct ref_store *ref
     +};
     +
      static int files_copy_or_rename_ref(struct ref_store *ref_store,
    - 			    const char *oldrefname, const char *newrefname,
    +-			    const char *oldrefname, const char *newrefname,
     -			    const char *logmsg, int copy)
    -+			    const char *logmsg, int copy,
    -+			    struct ref_transaction *transaction)
    ++				    struct ref_update *update,
    ++				    struct ref_transaction *transaction)
      {
      	struct files_ref_store *refs =
     -		files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
     +		files_downcast(ref_store, REF_STORE_WRITE,
     +			       "ref_transaction_prepare");
    ++	struct ref_copy_or_rename_update *operation = update->copy_or_rename;
    ++	const char *oldrefname = operation->old_refname;
    ++	const char *newrefname = update->refname;
    ++	const char *logmsg = operation->logmsg;
    ++	bool copy = operation->type == REF_UPDATE_COPY;
      	struct object_id orig_oid;
      	int flag = 0, logmoved = 0;
      	struct ref_lock *lock;
    @@ refs/files-backend.c: static int files_copy_or_rename_ref(struct ref_store *ref_
      				    oldrefname);
      		goto out;
      	}
    -+	if (!oideq(&orig_oid, &transaction->source_oid)) {
    ++	if (!oideq(&orig_oid, &operation->source_oid)) {
     +		ret = error("refname %s is at %s but expected %s",
     +			    oldrefname, oid_to_hex(&orig_oid),
    -+			    oid_to_hex(&transaction->source_oid));
    ++			    oid_to_hex(&operation->source_oid));
     +		goto out;
     +	}
      	if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
    @@ refs/files-backend.c: static int files_copy_or_rename_ref(struct ref_store *ref_
     +			goto out;
     +		}
     +	}
    -+	if (destination_exists != transaction->destination_exists) {
    ++	if (destination_exists != operation->destination_exists) {
     +		ret = error("refname %s changed while renaming", newrefname);
     +		goto out;
     +	}
     +	if (destination_exists) {
     +		if (destination_flags & REF_ISSYMREF) {
    -+			if (!transaction->destination_target ||
    ++			if (!operation->destination_target ||
     +			    strcmp(destination_target.buf,
    -+				   transaction->destination_target)) {
    ++				   operation->destination_target)) {
     +				ret = error("refname %s changed while renaming",
     +					    newrefname);
     +				goto out;
     +			}
    -+		} else if (transaction->destination_target ||
    ++		} else if (operation->destination_target ||
     +			   !oideq(&destination_oid,
    -+				  &transaction->destination_oid)) {
    ++				  &operation->destination_oid)) {
     +			ret = error("refname %s changed while renaming", newrefname);
     +			goto out;
     +		}
    @@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref
      	struct ref_transaction *packed_transaction = NULL;
      
      	assert(err);
    -+	if (transaction->type != REF_TRANSACTION_TYPE_NORMAL)
    -+		return files_copy_or_rename_ref(ref_store,
    -+				transaction->old_refname,
    -+				transaction->new_refname,
    -+				transaction->logmsg,
    -+				transaction->type == REF_TRANSACTION_TYPE_COPY,
    -+				transaction);
    ++	{
    ++		struct ref_update *operation =
    ++			ref_transaction_copy_or_rename_update(transaction);
    ++
    ++		if (operation)
    ++			return files_copy_or_rename_ref(ref_store, operation,
    ++							transaction);
    ++	}
      
      	if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
      		goto cleanup;
    @@ refs/files-backend.c: static int files_transaction_finish(struct ref_store *ref_
      
      
      	assert(err);
    -+	if (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {
    -+		struct files_copy_or_rename_transaction_data *data =
    -+			transaction->backend_data;
    -+		int special_ret;
    ++	{
    ++		struct ref_update *update =
    ++			ref_transaction_copy_or_rename_update(transaction);
     +
    -+		special_ret = commit_ref_update(refs, data->lock, &data->orig_oid,
    -+						transaction->logmsg, 0, err);
    -+		if (special_ret) {
    -+			error("unable to write current sha1 into %s: %s",
    -+			      transaction->new_refname, err->buf);
    -+			data->lock = NULL;
    -+			files_transaction_abort(ref_store, transaction, err);
    -+			return special_ret;
    -+		} else if (data->destination_log_backed_up) {
    -+			struct strbuf path = STRBUF_INIT;
    ++		if (update) {
    ++			struct ref_copy_or_rename_update *operation =
    ++				update->copy_or_rename;
    ++			struct files_copy_or_rename_transaction_data *data =
    ++				transaction->backend_data;
    ++			int special_ret;
    ++
    ++			special_ret = commit_ref_update(refs, data->lock, &data->orig_oid,
    ++							operation->logmsg, 0, err);
    ++			if (special_ret) {
    ++				error("unable to write current sha1 into %s: %s",
    ++				      update->refname, err->buf);
    ++				data->lock = NULL;
    ++				files_transaction_abort(ref_store, transaction, err);
    ++				return special_ret;
    ++			} else if (data->destination_log_backed_up) {
    ++				struct strbuf path = STRBUF_INIT;
     +
    -+			files_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);
    -+			if (unlink(path.buf) < 0 && errno != ENOENT)
    -+				warning_errno("unable to remove '%s'", path.buf);
    -+			strbuf_release(&path);
    ++				files_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);
    ++				if (unlink(path.buf) < 0 && errno != ENOENT)
    ++					warning_errno("unable to remove '%s'", path.buf);
    ++				strbuf_release(&path);
    ++			}
    ++			free(data->destination_target);
    ++			free(data);
    ++			transaction->backend_data = NULL;
    ++			transaction->state = REF_TRANSACTION_CLOSED;
    ++			return special_ret;
     +		}
    -+		free(data->destination_target);
    -+		free(data);
    -+		transaction->backend_data = NULL;
    -+		transaction->state = REF_TRANSACTION_CLOSED;
    -+		return special_ret;
     +	}
      
      	if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
    @@ refs/files-backend.c: static int files_transaction_finish(struct ref_store *ref_
      	struct files_ref_store *refs =
      		files_downcast(ref_store, 0, "ref_transaction_abort");
      
    -+	if (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {
    -+		struct files_copy_or_rename_transaction_data *data =
    -+			transaction->backend_data;
    -+		struct strbuf new_log = STRBUF_INIT;
    -+		struct strbuf destination_log = STRBUF_INIT;
    -+		struct strbuf temporary_log = STRBUF_INIT;
    -+		struct ref_transaction *restore_transaction = NULL;
    -+		struct ref_lock *lock;
    -+		int ret = 0;
    ++	{
    ++		struct ref_update *update =
    ++			ref_transaction_copy_or_rename_update(transaction);
     +
    -+		if (data->lock)
    -+			unlock_ref(data->lock);
    -+		if (transaction->type == REF_TRANSACTION_TYPE_RENAME) {
    -+			lock = lock_ref_oid_basic(refs, transaction->old_refname, err);
    -+			if (!lock ||
    -+			    write_ref_to_lockfile(refs, lock, &data->orig_oid, err) ||
    -+			    commit_ref_update(refs, lock, &data->orig_oid, NULL,
    -+					      REF_SKIP_CREATE_REFLOG, err))
    -+				ret = -1;
    -+		}
    ++		if (update) {
    ++			struct ref_copy_or_rename_update *operation =
    ++				update->copy_or_rename;
    ++			struct files_copy_or_rename_transaction_data *data =
    ++				transaction->backend_data;
    ++			struct strbuf new_log = STRBUF_INIT;
    ++			struct strbuf destination_log = STRBUF_INIT;
    ++			struct strbuf temporary_log = STRBUF_INIT;
    ++			struct ref_transaction *restore_transaction = NULL;
    ++			struct ref_lock *lock;
    ++			int ret = 0;
     +
    -+		if (data->logmoved) {
    -+			files_reflog_path(refs, &new_log, transaction->new_refname);
    -+			if (transaction->type == REF_TRANSACTION_TYPE_RENAME) {
    -+				files_reflog_path(refs, &temporary_log, TMP_RENAMED_LOG);
    -+				if (rename(new_log.buf, temporary_log.buf) < 0) {
    -+					strbuf_addf(err, "unable to restore logfile %s: %s",
    -+						    transaction->old_refname, strerror(errno));
    ++			if (data->lock)
    ++				unlock_ref(data->lock);
    ++			if (operation->type == REF_UPDATE_RENAME) {
    ++				lock = lock_ref_oid_basic(refs, operation->old_refname, err);
    ++				if (!lock ||
    ++				    write_ref_to_lockfile(refs, lock, &data->orig_oid, err) ||
    ++				    commit_ref_update(refs, lock, &data->orig_oid, NULL,
    ++						      REF_SKIP_CREATE_REFLOG, err))
     +					ret = -1;
    -+				} else {
    -+					try_remove_empty_parents(refs,
    -+							 transaction->new_refname,
    -+							 REMOVE_EMPTY_PARENTS_REFLOG);
    -+					if (rename_tmp_log(refs,
    -+							   transaction->old_refname)) {
    ++			}
    ++
    ++			if (data->logmoved) {
    ++				files_reflog_path(refs, &new_log, update->refname);
    ++				if (operation->type == REF_UPDATE_RENAME) {
    ++					files_reflog_path(refs, &temporary_log, TMP_RENAMED_LOG);
    ++					if (rename(new_log.buf, temporary_log.buf) < 0) {
     +						strbuf_addf(err, "unable to restore logfile %s: %s",
    -+							    transaction->old_refname,
    -+							    strerror(errno));
    ++							    operation->old_refname, strerror(errno));
     +						ret = -1;
    ++					} else {
    ++						try_remove_empty_parents(refs,
    ++									 update->refname,
    ++									 REMOVE_EMPTY_PARENTS_REFLOG);
    ++						if (rename_tmp_log(refs,
    ++								   operation->old_refname)) {
    ++							strbuf_addf(err, "unable to restore logfile %s: %s",
    ++								    operation->old_refname,
    ++								    strerror(errno));
    ++							ret = -1;
    ++						}
     +					}
    ++				} else if (unlink(new_log.buf) < 0 && errno != ENOENT) {
    ++					strbuf_addf(err, "unable to remove logfile %s: %s",
    ++						    update->refname, strerror(errno));
    ++					ret = -1;
     +				}
    -+			} else if (unlink(new_log.buf) < 0 && errno != ENOENT) {
    -+				strbuf_addf(err, "unable to remove logfile %s: %s",
    -+					    transaction->new_refname, strerror(errno));
    -+				ret = -1;
     +			}
    -+		}
    -+		if (data->destination_log_backed_up) {
    -+			files_reflog_path(refs, &destination_log,
    -+					   TMP_RENAMED_LOG_DESTINATION);
    -+			if (rename(destination_log.buf, new_log.buf) < 0) {
    -+				strbuf_addf(err, "unable to restore logfile %s: %s",
    -+					    transaction->new_refname, strerror(errno));
    -+				ret = -1;
    ++			if (data->destination_log_backed_up) {
    ++				files_reflog_path(refs, &destination_log,
    ++						  TMP_RENAMED_LOG_DESTINATION);
    ++				if (rename(destination_log.buf, new_log.buf) < 0) {
    ++					strbuf_addf(err, "unable to restore logfile %s: %s",
    ++						    update->refname, strerror(errno));
    ++					ret = -1;
    ++				}
     +			}
    -+		}
     +
    -+		if (transaction->type == REF_TRANSACTION_TYPE_RENAME &&
    -+		    data->destination_exists) {
    -+			restore_transaction = ref_store_transaction_begin(
    ++			if (operation->type == REF_UPDATE_RENAME &&
    ++			    data->destination_exists) {
    ++				restore_transaction = ref_store_transaction_begin(
     +					&refs->base, REF_TRANSACTION_FLAG_SKIP_HOOK, err);
    -+			if (!restore_transaction ||
    -+			    ref_transaction_update(restore_transaction,
    -+						   transaction->new_refname,
    -+						   data->destination_target ? NULL :
    -+							&data->destination_oid,
    -+						   NULL,
    -+						   data->destination_target,
    -+						   NULL,
    -+						   REF_NO_DEREF |
    -+							REF_SKIP_CREATE_REFLOG,
    -+						   NULL, err) ||
    -+			    ref_transaction_commit(restore_transaction, err))
    -+				ret = -1;
    -+			ref_transaction_free(restore_transaction);
    -+		}
    ++				if (!restore_transaction ||
    ++				    ref_transaction_update(restore_transaction,
    ++							   update->refname,
    ++							   data->destination_target ? NULL :
    ++										      &data->destination_oid,
    ++							   NULL,
    ++							   data->destination_target,
    ++							   NULL,
    ++							   REF_NO_DEREF |
    ++								   REF_SKIP_CREATE_REFLOG,
    ++							   NULL, err) ||
    ++				    ref_transaction_commit(restore_transaction, err))
    ++					ret = -1;
    ++				ref_transaction_free(restore_transaction);
    ++			}
     +
    -+		strbuf_release(&destination_log);
    -+		strbuf_release(&temporary_log);
    -+		strbuf_release(&new_log);
    -+		free(data->destination_target);
    -+		free(data);
    -+		transaction->backend_data = NULL;
    -+		transaction->state = REF_TRANSACTION_CLOSED;
    -+		return ret;
    ++			strbuf_release(&destination_log);
    ++			strbuf_release(&temporary_log);
    ++			strbuf_release(&new_log);
    ++			free(data->destination_target);
    ++			free(data);
    ++			transaction->backend_data = NULL;
    ++			transaction->state = REF_TRANSACTION_CLOSED;
    ++			return ret;
    ++		}
     +	}
     +
      	files_transaction_cleanup(refs, transaction);
    @@ refs/packed-backend.c: struct ref_storage_be refs_be_packed = {
      	.read_raw_ref = packed_read_raw_ref,
     
      ## refs/refs-internal.h ##
    +@@ refs/refs-internal.h: struct ref_update {
    + 	 */
    + 	struct ref_update *parent_update;
    + 
    ++	/*
    ++	 * Copy and rename operations require backend-specific handling while
    ++	 * still exposing their logical updates to transaction hooks. Keep that
    ++	 * state on the destination update so it composes with other updates in
    ++	 * the transaction instead of making copy or rename a transaction-wide
    ++	 * property.
    ++	 */
    ++	struct ref_copy_or_rename_update *copy_or_rename;
    ++
    + 	const char refname[FLEX_ARRAY];
    + };
    + 
    ++enum ref_copy_or_rename_type {
    ++	REF_UPDATE_RENAME,
    ++	REF_UPDATE_COPY,
    ++};
    ++
    ++struct ref_copy_or_rename_update {
    ++	enum ref_copy_or_rename_type type;
    ++	char *old_refname;
    ++	char *logmsg;
    ++	struct object_id source_oid;
    ++	struct object_id destination_oid;
    ++	char *destination_target;
    ++	unsigned int destination_exists:1;
    ++};
    ++
    + int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
    + 		      struct object_id *oid, struct strbuf *referent,
    + 		      unsigned int *type, int *failure_errno);
     @@ refs/refs-internal.h: struct ref_update *ref_transaction_add_update(
      		const char *committer_info,
      		const char *msg);
    @@ refs/refs-internal.h: struct ref_update *ref_transaction_add_update(
      /*
       * Transaction states.
       *
    -@@ refs/refs-internal.h: enum ref_transaction_state {
    - 	REF_TRANSACTION_CLOSED   = 2
    - };
    - 
    -+enum ref_transaction_type {
    -+	REF_TRANSACTION_TYPE_NORMAL = 0,
    -+	REF_TRANSACTION_TYPE_RENAME,
    -+	REF_TRANSACTION_TYPE_COPY,
    -+};
    -+
    - /*
    -  * Data structure to hold indices of updates which were rejected, for batched
    -  * reference updates. While the updates themselves hold the rejection error,
     @@ refs/refs-internal.h: struct ref_transaction {
    - 	void *backend_data;
    - 	unsigned int flags;
      	uint64_t max_index;
    -+
    -+	/*
    -+	 * Rename and copy operations need backend-specific reflog handling.
    -+	 * Their logical updates still live in `updates`, so hooks see the
    -+	 * operation like any other reference transaction. The fields below
    -+	 * retain the state that backends verify after taking their locks.
    -+	 */
    -+	enum ref_transaction_type type;
    -+	char *old_refname;
    -+	char *new_refname;
    -+	char *logmsg;
    -+	struct object_id source_oid;
    -+	struct object_id destination_oid;
    -+	char *destination_target;
    -+	unsigned int destination_exists:1;
      };
      
    ++/* Suppress hooks for a transaction nested inside another refs operation. */
    ++#define REF_TRANSACTION_FLAG_SKIP_HOOK (1 << 2)
    ++
    ++struct ref_update *ref_transaction_copy_or_rename_update(
    ++	struct ref_transaction *transaction);
    ++
      /*
    +  * Check for entries in extras that are within the specified
    +  * directory, where dirname is a reference directory name including
     @@ refs/refs-internal.h: typedef int optimize_required_fn(struct ref_store *ref_store,
      				 struct refs_optimize_opts *opts,
      				 bool *required);
    @@ refs/reftable-backend.c: static int reftable_be_transaction_prepare(struct ref_s
      	size_t i;
      	int ret;
      
    -+	if (transaction->type != REF_TRANSACTION_TYPE_NORMAL)
    ++	if (ref_transaction_copy_or_rename_update(transaction))
     +		return reftable_be_copy_or_rename_prepare(ref_store, transaction,
     +							   err);
     +
    @@ refs/reftable-backend.c: static int reftable_be_transaction_abort(struct ref_sto
     -	struct reftable_transaction_data *tx_data = transaction->backend_data;
     +	struct reftable_transaction_data *tx_data;
     +
    -+	if (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {
    ++	if (ref_transaction_copy_or_rename_update(transaction)) {
     +		struct reftable_copy_or_rename_transaction_data *data =
     +			transaction->backend_data;
     +
    @@ refs/reftable-backend.c: static int reftable_be_transaction_finish(struct ref_st
     +	struct reftable_transaction_data *tx_data;
      	int ret = 0;
      
    -+	if (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {
    ++	if (ref_transaction_copy_or_rename_update(transaction)) {
     +		struct reftable_copy_or_rename_transaction_data *data =
     +			transaction->backend_data;
     +		int special_ret = reftable_addition_commit(data->addition);
    @@ refs/reftable-backend.c: struct write_create_symref_arg {
      	const char *newname;
      	const char *logmsg;
      	int delete_old;
    -+	struct ref_transaction *transaction;
    ++	struct ref_copy_or_rename_update *operation;
      };
      
      static int write_copy_table(struct reftable_writer *writer, void *cb_data)
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     +		ret = -1;
      		goto done;
      	}
    -+	if (arg->transaction) {
    ++	if (arg->operation) {
     +		struct object_id oid;
     +
     +		if (old_ref.value_type == REFTABLE_REF_VAL2)
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     +		else
     +			oidread(&oid, old_ref.value.val1,
     +				arg->refs->base.repo->hash_algo);
    -+		if (!oideq(&oid, &arg->transaction->source_oid)) {
    ++		if (!oideq(&oid, &arg->operation->source_oid)) {
     +			strbuf_addf(arg->err,
     +				    _("refname %s is at %s but expected %s"),
     +				    arg->oldname, oid_to_hex(&oid),
    -+				    oid_to_hex(&arg->transaction->source_oid));
    ++				    oid_to_hex(&arg->operation->source_oid));
     +			ret = -1;
     +			goto done;
     +		}
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     +					      &destination_ref);
     +		if (ret < 0)
     +			goto done;
    -+		if (arg->transaction->destination_exists != !ret) {
    ++		if (arg->operation->destination_exists != !ret) {
     +			strbuf_addf(arg->err,
     +				    _("refname %s changed while renaming"),
     +				    arg->newname);
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     +		}
     +		if (!ret) {
     +			if (destination_ref.value_type == REFTABLE_REF_SYMREF) {
    -+				if (!arg->transaction->destination_target ||
    ++				if (!arg->operation->destination_target ||
     +				    strcmp(destination_ref.value.symref,
    -+					   arg->transaction->destination_target)) {
    ++					   arg->operation->destination_target)) {
     +					strbuf_addf(arg->err,
     +						    _("refname %s changed while renaming"),
     +						    arg->newname);
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     +				else
     +					oidread(&oid, destination_ref.value.val1,
     +						arg->refs->base.repo->hash_algo);
    -+				if (arg->transaction->destination_target ||
    -+				    !oideq(&oid, &arg->transaction->destination_oid)) {
    ++				if (arg->operation->destination_target ||
    ++				    !oideq(&oid, &arg->operation->destination_oid)) {
     +					strbuf_addf(arg->err,
     +						    _("refname %s changed while renaming"),
     +						    arg->newname);
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     +		reftable_be_downcast(ref_store, REF_STORE_WRITE,
     +				     "ref_transaction_prepare");
     +	struct reftable_copy_or_rename_transaction_data *data = NULL;
    ++	struct ref_update *update =
    ++		ref_transaction_copy_or_rename_update(transaction);
    ++	struct ref_copy_or_rename_update *operation = update->copy_or_rename;
      	struct write_copy_arg arg = {
      		.refs = refs,
     -		.oldname = oldrefname,
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     -		.logmsg = logmsg,
     -		.delete_old = 1,
     +		.err = err,
    -+		.oldname = transaction->old_refname,
    -+		.newname = transaction->new_refname,
    -+		.logmsg = transaction->logmsg,
    -+		.delete_old = transaction->type == REF_TRANSACTION_TYPE_RENAME,
    -+		.transaction = transaction,
    ++		.oldname = operation->old_refname,
    ++		.newname = update->refname,
    ++		.logmsg = operation->logmsg,
    ++		.delete_old = operation->type == REF_UPDATE_RENAME,
    ++		.operation = operation,
      	};
      	int ret;
      
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
      		goto done;
     -
     -	ret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);
    -+	ret = backend_for(&arg.be, refs, transaction->new_refname,
    ++	ret = backend_for(&arg.be, refs, update->refname,
     +			  &arg.newname, 1);
      	if (ret)
      		goto done;
 refs.c                           | 161 ++++++++++++++---
 refs/debug.c                     |  25 ---
 refs/files-backend.c             | 297 +++++++++++++++++++++++++++----
 refs/packed-backend.c            |   2 -
 refs/refs-internal.h             |  47 +++--
 refs/reftable-backend.c          | 197 +++++++++++++++-----
 t/t1416-ref-transaction-hooks.sh | 142 +++++++++++++++
 7 files changed, 732 insertions(+), 139 deletions(-)
refs.c +138 -23
index 92d5df5b7..f036ae4b9 100644
@@ -1004,15 +1004,17 @@ long get_files_ref_lock_timeout_ms(struct repository *repo)
 	return timeout_ms;
 }
 
-int refs_delete_ref(struct ref_store *refs, const char *msg,
-		    const char *refname,
-		    const struct object_id *old_oid,
-		    unsigned int flags)
+int refs_delete_ref_with_transaction_flags(struct ref_store *refs,
+					   const char *msg,
+					   const char *refname,
+					   const struct object_id *old_oid,
+					   unsigned int flags,
+					   unsigned int transaction_flags)
 {
 	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
 
-	transaction = ref_store_transaction_begin(refs, 0, &err);
+	transaction = ref_store_transaction_begin(refs, transaction_flags, &err);
 	if (!transaction ||
 	    ref_transaction_delete(transaction, refname, old_oid,
 				   NULL, flags, msg, &err) ||
@@ -1027,6 +1029,15 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
 	return 0;
 }
 
+int refs_delete_ref(struct ref_store *refs, const char *msg,
+		    const char *refname,
+		    const struct object_id *old_oid,
+		    unsigned int flags)
+{
+	return refs_delete_ref_with_transaction_flags(refs, msg, refname,
+						      old_oid, flags, 0);
+}
+
 static void copy_reflog_msg(struct strbuf *sb, const char *msg)
 {
 	char c;
@@ -1256,11 +1267,20 @@ void ref_transaction_free(struct ref_transaction *transaction)
 	}
 
 	for (i = 0; i < transaction->nr; i++) {
+		struct ref_copy_or_rename_update *operation =
+			transaction->updates[i]->copy_or_rename;
+
 		free(transaction->updates[i]->msg);
 		free(transaction->updates[i]->committer_info);
 		free((char *)transaction->updates[i]->new_target);
 		free((char *)transaction->updates[i]->old_target);
 		free((char *)transaction->updates[i]->rejection_details);
+		if (operation) {
+			free(operation->old_refname);
+			free(operation->logmsg);
+			free(operation->destination_target);
+			free(operation);
+		}
 		free(transaction->updates[i]);
 	}
 
@@ -1273,6 +1293,23 @@ void ref_transaction_free(struct ref_transaction *transaction)
 	free(transaction);
 }
 
+struct ref_update *ref_transaction_copy_or_rename_update(
+	struct ref_transaction *transaction)
+{
+	struct ref_update *operation = NULL;
+	size_t i;
+
+	for (i = 0; i < transaction->nr; i++) {
+		if (!transaction->updates[i]->copy_or_rename)
+			continue;
+		if (operation)
+			BUG("multiple copy or rename updates in one transaction");
+		operation = transaction->updates[i];
+	}
+
+	return operation;
+}
+
 int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,
 				       size_t update_idx,
 				       enum ref_transaction_error err,
@@ -2710,7 +2747,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,
 		return REF_TRANSACTION_ERROR_GENERIC;
 
 	/* Preparing checks before locking references */
-	ret = run_transaction_hook(transaction, "preparing");
+	ret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :
+		run_transaction_hook(transaction, "preparing");
 	if (ret) {
 		ref_transaction_abort(transaction, err);
 		die(_(abort_by_ref_transaction_hook), "preparing");
@@ -2720,7 +2758,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,
 	if (ret)
 		return ret;
 
-	ret = run_transaction_hook(transaction, "prepared");
+	ret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :
+		run_transaction_hook(transaction, "prepared");
 	if (ret) {
 		ref_transaction_abort(transaction, err);
 		die(_(abort_by_ref_transaction_hook), "prepared");
@@ -2750,7 +2789,8 @@ int ref_transaction_abort(struct ref_transaction *transaction,
 		break;
 	}
 
-	run_transaction_hook(transaction, "aborted");
+	if (!(transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK))
+		run_transaction_hook(transaction, "aborted");
 
 	ref_transaction_free(transaction);
 	return ret;
@@ -2781,7 +2821,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 	}
 
 	ret = refs->be->transaction_finish(refs, transaction, err);
-	if (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL))
+	if (!ret && !(transaction->flags & (REF_TRANSACTION_FLAG_INITIAL |
+					 REF_TRANSACTION_FLAG_SKIP_HOOK)))
 		run_transaction_hook(transaction, "committed");
 	return ret;
 }
@@ -3123,28 +3164,102 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,
 	return ret;
 }
 
-int refs_rename_ref(struct ref_store *refs, const char *oldref,
-		    const char *newref, const char *logmsg)
+static int refs_copy_or_rename_ref(struct ref_store *refs, const char *oldref,
+				   const char *newref, const char *logmsg,
+				   bool copy)
 {
-	char *msg;
-	int retval;
+	struct ref_transaction *transaction = NULL;
+	struct ref_copy_or_rename_update *operation = NULL;
+	struct ref_update *destination_update;
+	struct object_id old_oid, new_oid;
+	struct strbuf new_target = STRBUF_INIT;
+	struct strbuf err = STRBUF_INIT;
+	char *msg = normalize_reflog_message(logmsg);
+	int old_flags, new_flags = 0, new_exists = 0, ret = 1;
 
-	msg = normalize_reflog_message(logmsg);
-	retval = refs->be->rename_ref(refs, oldref, newref, msg);
+	if (!strcmp(oldref, newref)) {
+		ret = 0;
+		goto out;
+	}
+
+	if (!refs_resolve_ref_unsafe(refs, oldref,
+				     RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
+				     &old_oid, &old_flags)) {
+		error("refname %s not found", oldref);
+		goto out;
+	}
+	if (old_flags & REF_ISSYMREF) {
+		error("refname %s is a symbolic ref, %s it is not supported",
+		      oldref, copy ? "copying" : "renaming");
+		goto out;
+	}
+
+	transaction = ref_store_transaction_begin(refs, 0, &err);
+	if (!transaction)
+		goto error;
+	if (!copy && ref_transaction_delete(transaction, oldref, &old_oid, NULL,
+					    REF_NO_DEREF, msg, &err))
+		goto error;
+
+	if (refs_resolve_ref_unsafe(refs, newref,
+				    RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
+				    &new_oid, &new_flags)) {
+		new_exists = 1;
+		if ((new_flags & REF_ISSYMREF) &&
+		    refs_read_symbolic_ref(refs, newref, &new_target) < 0) {
+			strbuf_addf(&err, "unable to read symbolic ref %s", newref);
+			goto error;
+		}
+	} else {
+		oidclr(&new_oid, refs->repo->hash_algo);
+	}
+	if (ref_transaction_update(transaction, newref, &old_oid,
+				   (new_flags & REF_ISSYMREF) ? NULL : &new_oid,
+				   NULL,
+				   (new_flags & REF_ISSYMREF) ? new_target.buf : NULL,
+				   REF_NO_DEREF | REF_SKIP_CREATE_REFLOG,
+				   NULL, &err))
+		goto error;
+
+	destination_update = transaction->updates[transaction->nr - 1];
+	CALLOC_ARRAY(operation, 1);
+	operation->type = copy ? REF_UPDATE_COPY : REF_UPDATE_RENAME;
+	operation->old_refname = xstrdup(oldref);
+	operation->logmsg = xstrdup(msg);
+	oidcpy(&operation->source_oid, &old_oid);
+	operation->destination_exists = new_exists;
+	if (new_flags & REF_ISSYMREF)
+		operation->destination_target = xstrdup(new_target.buf);
+	else if (operation->destination_exists)
+		oidcpy(&operation->destination_oid, &new_oid);
+	destination_update->copy_or_rename = operation;
+
+	if (ref_transaction_commit(transaction, &err))
+		goto error;
+
+	ret = 0;
+	goto out;
+
+error:
+	error("%s", err.buf);
+out:
+	ref_transaction_free(transaction);
+	strbuf_release(&new_target);
+	strbuf_release(&err);
 	free(msg);
-	return retval;
+	return ret;
 }
 
-int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
+int refs_rename_ref(struct ref_store *refs, const char *oldref,
 		    const char *newref, const char *logmsg)
 {
-	char *msg;
-	int retval;
+	return refs_copy_or_rename_ref(refs, oldref, newref, logmsg, 0);
+}
 
-	msg = normalize_reflog_message(logmsg);
-	retval = refs->be->copy_ref(refs, oldref, newref, msg);
-	free(msg);
-	return retval;
+int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
+		    const char *newref, const char *logmsg)
+{
+	return refs_copy_or_rename_ref(refs, oldref, newref, logmsg, 1);
 }
 
 const char *ref_update_original_update_refname(struct ref_update *update)
refs/debug.c +0 -25
index 639db0f26..87b84e767 100644
@@ -143,28 +143,6 @@ static int debug_optimize_required(struct ref_store *ref_store,
 	return res;
 }
 
-static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,
-			    const char *newref, const char *logmsg)
-{
-	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref,
-					      logmsg);
-	trace_printf_key(&trace_refs, "rename_ref: %s -> %s \"%s\": %d\n", oldref, newref,
-		logmsg, res);
-	return res;
-}
-
-static int debug_copy_ref(struct ref_store *ref_store, const char *oldref,
-			  const char *newref, const char *logmsg)
-{
-	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res =
-		drefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg);
-	trace_printf_key(&trace_refs, "copy_ref: %s -> %s \"%s\": %d\n", oldref, newref,
-		logmsg, res);
-	return res;
-}
-
 struct debug_ref_iterator {
 	struct ref_iterator base;
 	struct ref_iterator *iter;
@@ -453,9 +431,6 @@ struct ref_storage_be refs_be_debug = {
 	.optimize = debug_optimize,
 	.optimize_required = debug_optimize_required,
 
-	.rename_ref = debug_rename_ref,
-	.copy_ref = debug_copy_ref,
-
 	.iterator_begin = debug_ref_iterator_begin,
 	.read_raw_ref = debug_read_raw_ref,
 	.read_symbolic_ref = debug_read_symbolic_ref,
refs/files-backend.c +266 -31

411 lines not on this page · raw message

refs/packed-backend.c +0 -2
index a73fc6aca..364a91291 100644
@@ -2164,8 +2164,6 @@ struct ref_storage_be refs_be_packed = {
 	.optimize = packed_optimize,
 	.optimize_required = packed_optimize_required,
 
-	.rename_ref = NULL,
-	.copy_ref = NULL,
 
 	.iterator_begin = packed_ref_iterator_begin,
 	.read_raw_ref = packed_read_raw_ref,
refs/refs-internal.h +37 -10

80 lines not on this page · raw message

refs/reftable-backend.c +149 -48

291 lines not on this page · raw message

t/t1416-ref-transaction-hooks.sh +142 -0

148 lines not on this page · raw message

Signature · 1 line
2.39.3 (Apple Git-146)
#7 lore raw

Re: [PATCH v2] refs: run copy and rename through transactions

I'd appreciate a code review of v2.

Thanks,
- Maciej Ciemborowicz

Quoted 2,045 lines · Maciej Ciemborowicz

Reference copy and rename operations bypass the transaction API. Consequently, the reference-transaction hook sees only the source deletion with the files backend and no useful update with the reftable backend.

Represent both operations as reference transactions containing their logical updates. A rename is a deletion of the old reference and creation of the new reference in the same transaction. Attach operation-specific state to the destination update instead of making copy or rename a property of the entire transaction.

Retain backend-specific reflog handling: the files backend stages its existing rename procedure across prepare, finish and abort, while reftable stages an addition while holding the stack lock. Suppress hooks for the files backend's nested deletion transactions so that callers observe one logical transaction.

Record and verify the source and destination values after taking backend locks. This rejects concurrent changes instead of applying a rename or copy that differs from the payload shown to the preparing hook. Preserve D/F renames and restore overwritten references and reflogs when a prepared hook rejects the operation.

Add tests covering rename, copy, forced updates, both directions of D/F conflicts, concurrent updates and prepared-hook rollback.

---
Apologies for the unrelated Subject header on my earlier reply. This reroll incorporates the points discussed there.

Changes since v1:

* Keep copy/rename state on the destination ref update instead of the generic transaction, so the operation is no longer a transaction-wide property.
* Keep REF_TRANSACTION_FLAG_SKIP_HOOK private to the refs implementation.
* Use bool for the two-state copy parameter.
* Apply Junio's commit-message wording suggestions.

Range-diff against v1:
1: de0a5a9f7 ! 1: d852537d8 refs: run copy and rename through transactions

    @@ Metadata
      ## Commit message ##
         refs: run copy and rename through transactions

    -    Reference copy and rename operations currently bypass the transaction API.
    +    Reference copy and rename operations bypass the transaction API.
         Consequently, the reference-transaction hook sees only the source deletion
         with the files backend and no useful update with the reftable backend.

         Represent both operations as reference transactions containing their
         logical updates. A rename is a deletion of the old reference and creation
    -    of the new reference in the same transaction. Retain backend-specific
    -    reflog handling: the files backend stages its existing rename procedure
    -    across prepare, finish and abort, while reftable stages an addition while
    -    holding the stack lock. Suppress hooks for the files backend's nested
    -    deletion transactions so that callers observe one logical transaction.
    +    of the new reference in the same transaction. Attach operation-specific
    +    state to the destination update instead of making copy or rename a property
    +    of the entire transaction.
    +
    +    Retain backend-specific reflog handling: the files backend stages its
    +    existing rename procedure across prepare, finish and abort, while reftable
    +    stages an addition while holding the stack lock. Suppress hooks for the
    +    files backend's nested deletion transactions so that callers observe one
    +    logical transaction.

         Record and verify the source and destination values after taking backend
         locks. This rejects concurrent changes instead of applying a rename or copy
    @@ Commit message
         renames and restore overwritten references and reflogs when a prepared hook
         rejects the operation.

    -    Add coverage for rename, copy, forced updates, both directions of D/F
    +    Add tests covering rename, copy, forced updates, both directions of D/F
         conflicts, concurrent updates and prepared-hook rollback.

         Helped-by: Karthik Nayak <[email protected]>
    @@ refs.c: int refs_delete_ref(struct ref_store *refs, const char *msg,
      {
        char c;
     @@ refs.c: void ref_transaction_free(struct ref_transaction *transaction)
    +   }
    +
    +   for (i = 0; i < transaction->nr; i++) {
    ++          struct ref_copy_or_rename_update *operation =
    ++                  transaction->updates[i]->copy_or_rename;
    ++
    +           free(transaction->updates[i]->msg);
    +           free(transaction->updates[i]->committer_info);
    +           free((char *)transaction->updates[i]->new_target);
    +           free((char *)transaction->updates[i]->old_target);
    +           free((char *)transaction->updates[i]->rejection_details);
    ++          if (operation) {
    ++                  free(operation->old_refname);
    ++                  free(operation->logmsg);
    ++                  free(operation->destination_target);
    ++                  free(operation);
    ++          }
    +           free(transaction->updates[i]);
    +   }

    -   string_list_clear(&transaction->refnames, 0);
    -   free(transaction->updates);
    -+  free(transaction->old_refname);
    -+  free(transaction->new_refname);
    -+  free(transaction->logmsg);
    -+  free(transaction->destination_target);
    +@@ refs.c: void ref_transaction_free(struct ref_transaction *transaction)
        free(transaction);
      }

    ++struct ref_update *ref_transaction_copy_or_rename_update(
    ++  struct ref_transaction *transaction)
    ++{
    ++  struct ref_update *operation = NULL;
    ++  size_t i;
    ++
    ++  for (i = 0; i < transaction->nr; i++) {
    ++          if (!transaction->updates[i]->copy_or_rename)
    ++                  continue;
    ++          if (operation)
    ++                  BUG("multiple copy or rename updates in one transaction");
    ++          operation = transaction->updates[i];
    ++  }
    ++
    ++  return operation;
    ++}
    ++
    + int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,
    +                                  size_t update_idx,
    +                                  enum ref_transaction_error err,
     @@ refs.c: int ref_transaction_prepare(struct ref_transaction *transaction,
                return REF_TRANSACTION_ERROR_GENERIC;

    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,
     -              const char *newref, const char *logmsg)
     +static int refs_copy_or_rename_ref(struct ref_store *refs, const char *oldref,
     +                             const char *newref, const char *logmsg,
    -+                             int copy)
    ++                             bool copy)
      {
     -  char *msg;
     -  int retval;
     +  struct ref_transaction *transaction = NULL;
    ++  struct ref_copy_or_rename_update *operation = NULL;
    ++  struct ref_update *destination_update;
     +  struct object_id old_oid, new_oid;
     +  struct strbuf new_target = STRBUF_INIT;
     +  struct strbuf err = STRBUF_INIT;
    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,
     +  transaction = ref_store_transaction_begin(refs, 0, &err);
     +  if (!transaction)
     +          goto error;
    -+  transaction->type = copy ? REF_TRANSACTION_TYPE_COPY :
    -+          REF_TRANSACTION_TYPE_RENAME;
    -+  transaction->old_refname = xstrdup(oldref);
    -+  transaction->new_refname = xstrdup(newref);
    -+  transaction->logmsg = xstrdup(msg);
    -+  oidcpy(&transaction->source_oid, &old_oid);
    -+
     +  if (!copy && ref_transaction_delete(transaction, oldref, &old_oid, NULL,
     +                                      REF_NO_DEREF, msg, &err))
     +          goto error;
    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,
     +  } else {
     +          oidclr(&new_oid, refs->repo->hash_algo);
     +  }
    -+  transaction->destination_exists = new_exists;
    -+  if (new_flags & REF_ISSYMREF)
    -+          transaction->destination_target = xstrdup(new_target.buf);
    -+  else if (transaction->destination_exists)
    -+          oidcpy(&transaction->destination_oid, &new_oid);
    -+
     +  if (ref_transaction_update(transaction, newref, &old_oid,
     +                             (new_flags & REF_ISSYMREF) ? NULL : &new_oid,
     +                             NULL,
    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,
     +                             NULL, &err))
     +          goto error;
     +
    ++  destination_update = transaction->updates[transaction->nr - 1];
    ++  CALLOC_ARRAY(operation, 1);
    ++  operation->type = copy ? REF_UPDATE_COPY : REF_UPDATE_RENAME;
    ++  operation->old_refname = xstrdup(oldref);
    ++  operation->logmsg = xstrdup(msg);
    ++  oidcpy(&operation->source_oid, &old_oid);
    ++  operation->destination_exists = new_exists;
    ++  if (new_flags & REF_ISSYMREF)
    ++          operation->destination_target = xstrdup(new_target.buf);
    ++  else if (operation->destination_exists)
    ++          oidcpy(&operation->destination_oid, &new_oid);
    ++  destination_update->copy_or_rename = operation;
    ++
     +  if (ref_transaction_commit(transaction, &err))
     +          goto error;
     +
    @@ refs.c: int refs_delete_refs(struct ref_store *refs, const char *logmsg,

      const char *ref_update_original_update_refname(struct ref_update *update)

    - ## refs.h ##
    -@@ refs.h: enum ref_transaction_flag {
    -    * while rejecting updates which do not match the expected state.
    -    */
    -   REF_TRANSACTION_ALLOW_FAILURE = (1 << 1),
    -+
    -+  /* Suppress hooks for an update nested in another transaction. */
    -+  REF_TRANSACTION_FLAG_SKIP_HOOK = (1 << 2),
    - };
    -
    - /*
    -
      ## refs/debug.c ##
     @@ refs/debug.c: static int debug_optimize_required(struct ref_store *ref_store,
        return res;
    @@ refs/files-backend.c: static int refs_rename_ref_available(struct ref_store *ref
     +};
     +
      static int files_copy_or_rename_ref(struct ref_store *ref_store,
    -                       const char *oldrefname, const char *newrefname,
    +-                      const char *oldrefname, const char *newrefname,
     -                      const char *logmsg, int copy)
    -+                      const char *logmsg, int copy,
    -+                      struct ref_transaction *transaction)
    ++                              struct ref_update *update,
    ++                              struct ref_transaction *transaction)
      {
        struct files_ref_store *refs =
     -          files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
     +          files_downcast(ref_store, REF_STORE_WRITE,
     +                         "ref_transaction_prepare");
    ++  struct ref_copy_or_rename_update *operation = update->copy_or_rename;
    ++  const char *oldrefname = operation->old_refname;
    ++  const char *newrefname = update->refname;
    ++  const char *logmsg = operation->logmsg;
    ++  bool copy = operation->type == REF_UPDATE_COPY;
        struct object_id orig_oid;
        int flag = 0, logmoved = 0;
        struct ref_lock *lock;
    @@ refs/files-backend.c: static int files_copy_or_rename_ref(struct ref_store *ref_
                                    oldrefname);
                goto out;
        }
    -+  if (!oideq(&orig_oid, &transaction->source_oid)) {
    ++  if (!oideq(&orig_oid, &operation->source_oid)) {
     +          ret = error("refname %s is at %s but expected %s",
     +                      oldrefname, oid_to_hex(&orig_oid),
    -+                      oid_to_hex(&transaction->source_oid));
    ++                      oid_to_hex(&operation->source_oid));
     +          goto out;
     +  }
        if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
    @@ refs/files-backend.c: static int files_copy_or_rename_ref(struct ref_store *ref_
     +                  goto out;
     +          }
     +  }
    -+  if (destination_exists != transaction->destination_exists) {
    ++  if (destination_exists != operation->destination_exists) {
     +          ret = error("refname %s changed while renaming", newrefname);
     +          goto out;
     +  }
     +  if (destination_exists) {
     +          if (destination_flags & REF_ISSYMREF) {
    -+                  if (!transaction->destination_target ||
    ++                  if (!operation->destination_target ||
     +                      strcmp(destination_target.buf,
    -+                             transaction->destination_target)) {
    ++                             operation->destination_target)) {
     +                          ret = error("refname %s changed while renaming",
     +                                      newrefname);
     +                          goto out;
     +                  }
    -+          } else if (transaction->destination_target ||
    ++          } else if (operation->destination_target ||
     +                     !oideq(&destination_oid,
    -+                            &transaction->destination_oid)) {
    ++                            &operation->destination_oid)) {
     +                  ret = error("refname %s changed while renaming", newrefname);
     +                  goto out;
     +          }
    @@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref
        struct ref_transaction *packed_transaction = NULL;

        assert(err);
    -+  if (transaction->type != REF_TRANSACTION_TYPE_NORMAL)
    -+          return files_copy_or_rename_ref(ref_store,
    -+                          transaction->old_refname,
    -+                          transaction->new_refname,
    -+                          transaction->logmsg,
    -+                          transaction->type == REF_TRANSACTION_TYPE_COPY,
    -+                          transaction);
    ++  {
    ++          struct ref_update *operation =
    ++                  ref_transaction_copy_or_rename_update(transaction);
    ++
    ++          if (operation)
    ++                  return files_copy_or_rename_ref(ref_store, operation,
    ++                                                  transaction);
    ++  }

        if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
                goto cleanup;
    @@ refs/files-backend.c: static int files_transaction_finish(struct ref_store *ref_


        assert(err);
    -+  if (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {
    -+          struct files_copy_or_rename_transaction_data *data =
    -+                  transaction->backend_data;
    -+          int special_ret;
    ++  {
    ++          struct ref_update *update =
    ++                  ref_transaction_copy_or_rename_update(transaction);
     +
    -+          special_ret = commit_ref_update(refs, data->lock, &data->orig_oid,
    -+                                          transaction->logmsg, 0, err);
    -+          if (special_ret) {
    -+                  error("unable to write current sha1 into %s: %s",
    -+                        transaction->new_refname, err->buf);
    -+                  data->lock = NULL;
    -+                  files_transaction_abort(ref_store, transaction, err);
    -+                  return special_ret;
    -+          } else if (data->destination_log_backed_up) {
    -+                  struct strbuf path = STRBUF_INIT;
    ++          if (update) {
    ++                  struct ref_copy_or_rename_update *operation =
    ++                          update->copy_or_rename;
    ++                  struct files_copy_or_rename_transaction_data *data =
    ++                          transaction->backend_data;
    ++                  int special_ret;
    ++
    ++                  special_ret = commit_ref_update(refs, data->lock, &data->orig_oid,
    ++                                                  operation->logmsg, 0, err);
    ++                  if (special_ret) {
    ++                          error("unable to write current sha1 into %s: %s",
    ++                                update->refname, err->buf);
    ++                          data->lock = NULL;
    ++                          files_transaction_abort(ref_store, transaction, err);
    ++                          return special_ret;
    ++                  } else if (data->destination_log_backed_up) {
    ++                          struct strbuf path = STRBUF_INIT;
     +
    -+                  files_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);
    -+                  if (unlink(path.buf) < 0 && errno != ENOENT)
    -+                          warning_errno("unable to remove '%s'", path.buf);
    -+                  strbuf_release(&path);
    ++                          files_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);
    ++                          if (unlink(path.buf) < 0 && errno != ENOENT)
    ++                                  warning_errno("unable to remove '%s'", path.buf);
    ++                          strbuf_release(&path);
    ++                  }
    ++                  free(data->destination_target);
    ++                  free(data);
    ++                  transaction->backend_data = NULL;
    ++                  transaction->state = REF_TRANSACTION_CLOSED;
    ++                  return special_ret;
     +          }
    -+          free(data->destination_target);
    -+          free(data);
    -+          transaction->backend_data = NULL;
    -+          transaction->state = REF_TRANSACTION_CLOSED;
    -+          return special_ret;
     +  }

        if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
    @@ refs/files-backend.c: static int files_transaction_finish(struct ref_store *ref_
        struct files_ref_store *refs =
                files_downcast(ref_store, 0, "ref_transaction_abort");

    -+  if (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {
    -+          struct files_copy_or_rename_transaction_data *data =
    -+                  transaction->backend_data;
    -+          struct strbuf new_log = STRBUF_INIT;
    -+          struct strbuf destination_log = STRBUF_INIT;
    -+          struct strbuf temporary_log = STRBUF_INIT;
    -+          struct ref_transaction *restore_transaction = NULL;
    -+          struct ref_lock *lock;
    -+          int ret = 0;
    ++  {
    ++          struct ref_update *update =
    ++                  ref_transaction_copy_or_rename_update(transaction);
     +
    -+          if (data->lock)
    -+                  unlock_ref(data->lock);
    -+          if (transaction->type == REF_TRANSACTION_TYPE_RENAME) {
    -+                  lock = lock_ref_oid_basic(refs, transaction->old_refname, err);
    -+                  if (!lock ||
    -+                      write_ref_to_lockfile(refs, lock, &data->orig_oid, err) ||
    -+                      commit_ref_update(refs, lock, &data->orig_oid, NULL,
    -+                                        REF_SKIP_CREATE_REFLOG, err))
    -+                          ret = -1;
    -+          }
    ++          if (update) {
    ++                  struct ref_copy_or_rename_update *operation =
    ++                          update->copy_or_rename;
    ++                  struct files_copy_or_rename_transaction_data *data =
    ++                          transaction->backend_data;
    ++                  struct strbuf new_log = STRBUF_INIT;
    ++                  struct strbuf destination_log = STRBUF_INIT;
    ++                  struct strbuf temporary_log = STRBUF_INIT;
    ++                  struct ref_transaction *restore_transaction = NULL;
    ++                  struct ref_lock *lock;
    ++                  int ret = 0;
     +
    -+          if (data->logmoved) {
    -+                  files_reflog_path(refs, &new_log, transaction->new_refname);
    -+                  if (transaction->type == REF_TRANSACTION_TYPE_RENAME) {
    -+                          files_reflog_path(refs, &temporary_log, TMP_RENAMED_LOG);
    -+                          if (rename(new_log.buf, temporary_log.buf) < 0) {
    -+                                  strbuf_addf(err, "unable to restore logfile %s: %s",
    -+                                              transaction->old_refname, strerror(errno));
    ++                  if (data->lock)
    ++                          unlock_ref(data->lock);
    ++                  if (operation->type == REF_UPDATE_RENAME) {
    ++                          lock = lock_ref_oid_basic(refs, operation->old_refname, err);
    ++                          if (!lock ||
    ++                              write_ref_to_lockfile(refs, lock, &data->orig_oid, err) ||
    ++                              commit_ref_update(refs, lock, &data->orig_oid, NULL,
    ++                                                REF_SKIP_CREATE_REFLOG, err))
     +                                  ret = -1;
    -+                          } else {
    -+                                  try_remove_empty_parents(refs,
    -+                                                   transaction->new_refname,
    -+                                                   REMOVE_EMPTY_PARENTS_REFLOG);
    -+                                  if (rename_tmp_log(refs,
    -+                                                     transaction->old_refname)) {
    ++                  }
    ++
    ++                  if (data->logmoved) {
    ++                          files_reflog_path(refs, &new_log, update->refname);
    ++                          if (operation->type == REF_UPDATE_RENAME) {
    ++                                  files_reflog_path(refs, &temporary_log, TMP_RENAMED_LOG);
    ++                                  if (rename(new_log.buf, temporary_log.buf) < 0) {
     +                                          strbuf_addf(err, "unable to restore logfile %s: %s",
    -+                                                      transaction->old_refname,
    -+                                                      strerror(errno));
    ++                                                      operation->old_refname, strerror(errno));
     +                                          ret = -1;
    ++                                  } else {
    ++                                          try_remove_empty_parents(refs,
    ++                                                                   update->refname,
    ++                                                                   REMOVE_EMPTY_PARENTS_REFLOG);
    ++                                          if (rename_tmp_log(refs,
    ++                                                             operation->old_refname)) {
    ++                                                  strbuf_addf(err, "unable to restore logfile %s: %s",
    ++                                                              operation->old_refname,
    ++                                                              strerror(errno));
    ++                                                  ret = -1;
    ++                                          }
     +                                  }
    ++                          } else if (unlink(new_log.buf) < 0 && errno != ENOENT) {
    ++                                  strbuf_addf(err, "unable to remove logfile %s: %s",
    ++                                              update->refname, strerror(errno));
    ++                                  ret = -1;
     +                          }
    -+                  } else if (unlink(new_log.buf) < 0 && errno != ENOENT) {
    -+                          strbuf_addf(err, "unable to remove logfile %s: %s",
    -+                                      transaction->new_refname, strerror(errno));
    -+                          ret = -1;
     +                  }
    -+          }
    -+          if (data->destination_log_backed_up) {
    -+                  files_reflog_path(refs, &destination_log,
    -+                                     TMP_RENAMED_LOG_DESTINATION);
    -+                  if (rename(destination_log.buf, new_log.buf) < 0) {
    -+                          strbuf_addf(err, "unable to restore logfile %s: %s",
    -+                                      transaction->new_refname, strerror(errno));
    -+                          ret = -1;
    ++                  if (data->destination_log_backed_up) {
    ++                          files_reflog_path(refs, &destination_log,
    ++                                            TMP_RENAMED_LOG_DESTINATION);
    ++                          if (rename(destination_log.buf, new_log.buf) < 0) {
    ++                                  strbuf_addf(err, "unable to restore logfile %s: %s",
    ++                                              update->refname, strerror(errno));
    ++                                  ret = -1;
    ++                          }
     +                  }
    -+          }
     +
    -+          if (transaction->type == REF_TRANSACTION_TYPE_RENAME &&
    -+              data->destination_exists) {
    -+                  restore_transaction = ref_store_transaction_begin(
    ++                  if (operation->type == REF_UPDATE_RENAME &&
    ++                      data->destination_exists) {
    ++                          restore_transaction = ref_store_transaction_begin(
     +                                  &refs->base, REF_TRANSACTION_FLAG_SKIP_HOOK, err);
    -+                  if (!restore_transaction ||
    -+                      ref_transaction_update(restore_transaction,
    -+                                             transaction->new_refname,
    -+                                             data->destination_target ? NULL :
    -+                                                  &data->destination_oid,
    -+                                             NULL,
    -+                                             data->destination_target,
    -+                                             NULL,
    -+                                             REF_NO_DEREF |
    -+                                                  REF_SKIP_CREATE_REFLOG,
    -+                                             NULL, err) ||
    -+                      ref_transaction_commit(restore_transaction, err))
    -+                          ret = -1;
    -+                  ref_transaction_free(restore_transaction);
    -+          }
    ++                          if (!restore_transaction ||
    ++                              ref_transaction_update(restore_transaction,
    ++                                                     update->refname,
    ++                                                     data->destination_target ? NULL :
    ++                                                                                &data->destination_oid,
    ++                                                     NULL,
    ++                                                     data->destination_target,
    ++                                                     NULL,
    ++                                                     REF_NO_DEREF |
    ++                                                             REF_SKIP_CREATE_REFLOG,
    ++                                                     NULL, err) ||
    ++                              ref_transaction_commit(restore_transaction, err))
    ++                                  ret = -1;
    ++                          ref_transaction_free(restore_transaction);
    ++                  }
     +
    -+          strbuf_release(&destination_log);
    -+          strbuf_release(&temporary_log);
    -+          strbuf_release(&new_log);
    -+          free(data->destination_target);
    -+          free(data);
    -+          transaction->backend_data = NULL;
    -+          transaction->state = REF_TRANSACTION_CLOSED;
    -+          return ret;
    ++                  strbuf_release(&destination_log);
    ++                  strbuf_release(&temporary_log);
    ++                  strbuf_release(&new_log);
    ++                  free(data->destination_target);
    ++                  free(data);
    ++                  transaction->backend_data = NULL;
    ++                  transaction->state = REF_TRANSACTION_CLOSED;
    ++                  return ret;
    ++          }
     +  }
     +
        files_transaction_cleanup(refs, transaction);
    @@ refs/packed-backend.c: struct ref_storage_be refs_be_packed = {
        .read_raw_ref = packed_read_raw_ref,

      ## refs/refs-internal.h ##
    +@@ refs/refs-internal.h: struct ref_update {
    +    */
    +   struct ref_update *parent_update;
    +
    ++  /*
    ++   * Copy and rename operations require backend-specific handling while
    ++   * still exposing their logical updates to transaction hooks. Keep that
    ++   * state on the destination update so it composes with other updates in
    ++   * the transaction instead of making copy or rename a transaction-wide
    ++   * property.
    ++   */
    ++  struct ref_copy_or_rename_update *copy_or_rename;
    ++
    +   const char refname[FLEX_ARRAY];
    + };
    +
    ++enum ref_copy_or_rename_type {
    ++  REF_UPDATE_RENAME,
    ++  REF_UPDATE_COPY,
    ++};
    ++
    ++struct ref_copy_or_rename_update {
    ++  enum ref_copy_or_rename_type type;
    ++  char *old_refname;
    ++  char *logmsg;
    ++  struct object_id source_oid;
    ++  struct object_id destination_oid;
    ++  char *destination_target;
    ++  unsigned int destination_exists:1;
    ++};
    ++
    + int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
    +                 struct object_id *oid, struct strbuf *referent,
    +                 unsigned int *type, int *failure_errno);
     @@ refs/refs-internal.h: struct ref_update *ref_transaction_add_update(
                const char *committer_info,
                const char *msg);
    @@ refs/refs-internal.h: struct ref_update *ref_transaction_add_update(
      /*
       * Transaction states.
       *
    -@@ refs/refs-internal.h: enum ref_transaction_state {
    -   REF_TRANSACTION_CLOSED   = 2
    - };
    -
    -+enum ref_transaction_type {
    -+  REF_TRANSACTION_TYPE_NORMAL = 0,
    -+  REF_TRANSACTION_TYPE_RENAME,
    -+  REF_TRANSACTION_TYPE_COPY,
    -+};
    -+
    - /*
    -  * Data structure to hold indices of updates which were rejected, for batched
    -  * reference updates. While the updates themselves hold the rejection error,
     @@ refs/refs-internal.h: struct ref_transaction {
    -   void *backend_data;
    -   unsigned int flags;
        uint64_t max_index;
    -+
    -+  /*
    -+   * Rename and copy operations need backend-specific reflog handling.
    -+   * Their logical updates still live in `updates`, so hooks see the
    -+   * operation like any other reference transaction. The fields below
    -+   * retain the state that backends verify after taking their locks.
    -+   */
    -+  enum ref_transaction_type type;
    -+  char *old_refname;
    -+  char *new_refname;
    -+  char *logmsg;
    -+  struct object_id source_oid;
    -+  struct object_id destination_oid;
    -+  char *destination_target;
    -+  unsigned int destination_exists:1;
      };

    ++/* Suppress hooks for a transaction nested inside another refs operation. */
    ++#define REF_TRANSACTION_FLAG_SKIP_HOOK (1 << 2)
    ++
    ++struct ref_update *ref_transaction_copy_or_rename_update(
    ++  struct ref_transaction *transaction);
    ++
      /*
    +  * Check for entries in extras that are within the specified
    +  * directory, where dirname is a reference directory name including
     @@ refs/refs-internal.h: typedef int optimize_required_fn(struct ref_store *ref_store,
                                 struct refs_optimize_opts *opts,
                                 bool *required);
    @@ refs/reftable-backend.c: static int reftable_be_transaction_prepare(struct ref_s
        size_t i;
        int ret;

    -+  if (transaction->type != REF_TRANSACTION_TYPE_NORMAL)
    ++  if (ref_transaction_copy_or_rename_update(transaction))
     +          return reftable_be_copy_or_rename_prepare(ref_store, transaction,
     +                                                     err);
     +
    @@ refs/reftable-backend.c: static int reftable_be_transaction_abort(struct ref_sto
     -  struct reftable_transaction_data *tx_data = transaction->backend_data;
     +  struct reftable_transaction_data *tx_data;
     +
    -+  if (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {
    ++  if (ref_transaction_copy_or_rename_update(transaction)) {
     +          struct reftable_copy_or_rename_transaction_data *data =
     +                  transaction->backend_data;
     +
    @@ refs/reftable-backend.c: static int reftable_be_transaction_finish(struct ref_st
     +  struct reftable_transaction_data *tx_data;
        int ret = 0;

    -+  if (transaction->type != REF_TRANSACTION_TYPE_NORMAL) {
    ++  if (ref_transaction_copy_or_rename_update(transaction)) {
     +          struct reftable_copy_or_rename_transaction_data *data =
     +                  transaction->backend_data;
     +          int special_ret = reftable_addition_commit(data->addition);
    @@ refs/reftable-backend.c: struct write_create_symref_arg {
        const char *newname;
        const char *logmsg;
        int delete_old;
    -+  struct ref_transaction *transaction;
    ++  struct ref_copy_or_rename_update *operation;
      };

      static int write_copy_table(struct reftable_writer *writer, void *cb_data)
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     +          ret = -1;
                goto done;
        }
    -+  if (arg->transaction) {
    ++  if (arg->operation) {
     +          struct object_id oid;
     +
     +          if (old_ref.value_type == REFTABLE_REF_VAL2)
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     +          else
     +                  oidread(&oid, old_ref.value.val1,
     +                          arg->refs->base.repo->hash_algo);
    -+          if (!oideq(&oid, &arg->transaction->source_oid)) {
    ++          if (!oideq(&oid, &arg->operation->source_oid)) {
     +                  strbuf_addf(arg->err,
     +                              _("refname %s is at %s but expected %s"),
     +                              arg->oldname, oid_to_hex(&oid),
    -+                              oid_to_hex(&arg->transaction->source_oid));
    ++                              oid_to_hex(&arg->operation->source_oid));
     +                  ret = -1;
     +                  goto done;
     +          }
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     +                                        &destination_ref);
     +          if (ret < 0)
     +                  goto done;
    -+          if (arg->transaction->destination_exists != !ret) {
    ++          if (arg->operation->destination_exists != !ret) {
     +                  strbuf_addf(arg->err,
     +                              _("refname %s changed while renaming"),
     +                              arg->newname);
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     +          }
     +          if (!ret) {
     +                  if (destination_ref.value_type == REFTABLE_REF_SYMREF) {
    -+                          if (!arg->transaction->destination_target ||
    ++                          if (!arg->operation->destination_target ||
     +                              strcmp(destination_ref.value.symref,
    -+                                     arg->transaction->destination_target)) {
    ++                                     arg->operation->destination_target)) {
     +                                  strbuf_addf(arg->err,
     +                                              _("refname %s changed while renaming"),
     +                                              arg->newname);
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     +                          else
     +                                  oidread(&oid, destination_ref.value.val1,
     +                                          arg->refs->base.repo->hash_algo);
    -+                          if (arg->transaction->destination_target ||
    -+                              !oideq(&oid, &arg->transaction->destination_oid)) {
    ++                          if (arg->operation->destination_target ||
    ++                              !oideq(&oid, &arg->operation->destination_oid)) {
     +                                  strbuf_addf(arg->err,
     +                                              _("refname %s changed while renaming"),
     +                                              arg->newname);
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     +          reftable_be_downcast(ref_store, REF_STORE_WRITE,
     +                               "ref_transaction_prepare");
     +  struct reftable_copy_or_rename_transaction_data *data = NULL;
    ++  struct ref_update *update =
    ++          ref_transaction_copy_or_rename_update(transaction);
    ++  struct ref_copy_or_rename_update *operation = update->copy_or_rename;
        struct write_copy_arg arg = {
                .refs = refs,
     -          .oldname = oldrefname,
    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
     -          .logmsg = logmsg,
     -          .delete_old = 1,
     +          .err = err,
    -+          .oldname = transaction->old_refname,
    -+          .newname = transaction->new_refname,
    -+          .logmsg = transaction->logmsg,
    -+          .delete_old = transaction->type == REF_TRANSACTION_TYPE_RENAME,
    -+          .transaction = transaction,
    ++          .oldname = operation->old_refname,
    ++          .newname = update->refname,
    ++          .logmsg = operation->logmsg,
    ++          .delete_old = operation->type == REF_UPDATE_RENAME,
    ++          .operation = operation,
        };
        int ret;

    @@ refs/reftable-backend.c: static int write_copy_table(struct reftable_writer *wri
                goto done;
     -
     -  ret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);
    -+  ret = backend_for(&arg.be, refs, transaction->new_refname,
    ++  ret = backend_for(&arg.be, refs, update->refname,
     +                    &arg.newname, 1);
        if (ret)
                goto done;
 refs.c                           | 161 ++++++++++++++---
 refs/debug.c                     |  25 ---
 refs/files-backend.c             | 297 +++++++++++++++++++++++++++----
 refs/packed-backend.c            |   2 -
 refs/refs-internal.h             |  47 +++--
 refs/reftable-backend.c          | 197 +++++++++++++++-----
 t/t1416-ref-transaction-hooks.sh | 142 +++++++++++++++
 7 files changed, 732 insertions(+), 139 deletions(-)
refs.c +138 -23
index 92d5df5b7..f036ae4b9 100644
@@ -1004,15 +1004,17 @@ long get_files_ref_lock_timeout_ms(struct repository *repo)
        return timeout_ms;
 }
 
-int refs_delete_ref(struct ref_store *refs, const char *msg,
-                   const char *refname,
-                   const struct object_id *old_oid,
-                   unsigned int flags)
+int refs_delete_ref_with_transaction_flags(struct ref_store *refs,
+                                          const char *msg,
+                                          const char *refname,
+                                          const struct object_id *old_oid,
+                                          unsigned int flags,
+                                          unsigned int transaction_flags)
 {
        struct ref_transaction *transaction;
        struct strbuf err = STRBUF_INIT;
 
-       transaction = ref_store_transaction_begin(refs, 0, &err);
+       transaction = ref_store_transaction_begin(refs, transaction_flags, &err);
        if (!transaction ||
            ref_transaction_delete(transaction, refname, old_oid,
                                   NULL, flags, msg, &err) ||
@@ -1027,6 +1029,15 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
        return 0;
 }
 
+int refs_delete_ref(struct ref_store *refs, const char *msg,
+                   const char *refname,
+                   const struct object_id *old_oid,
+                   unsigned int flags)
+{
+       return refs_delete_ref_with_transaction_flags(refs, msg, refname,
+                                                     old_oid, flags, 0);
+}
+
 static void copy_reflog_msg(struct strbuf *sb, const char *msg)
 {
        char c;
@@ -1256,11 +1267,20 @@ void ref_transaction_free(struct ref_transaction *transaction)
        }
 
        for (i = 0; i < transaction->nr; i++) {
+               struct ref_copy_or_rename_update *operation =
+                       transaction->updates[i]->copy_or_rename;
+
                free(transaction->updates[i]->msg);
                free(transaction->updates[i]->committer_info);
                free((char *)transaction->updates[i]->new_target);
                free((char *)transaction->updates[i]->old_target);
                free((char *)transaction->updates[i]->rejection_details);
+               if (operation) {
+                       free(operation->old_refname);
+                       free(operation->logmsg);
+                       free(operation->destination_target);
+                       free(operation);
+               }
                free(transaction->updates[i]);
        }
 
@@ -1273,6 +1293,23 @@ void ref_transaction_free(struct ref_transaction *transaction)
        free(transaction);
 }
 
+struct ref_update *ref_transaction_copy_or_rename_update(
+       struct ref_transaction *transaction)
+{
+       struct ref_update *operation = NULL;
+       size_t i;
+
+       for (i = 0; i < transaction->nr; i++) {
+               if (!transaction->updates[i]->copy_or_rename)
+                       continue;
+               if (operation)
+                       BUG("multiple copy or rename updates in one transaction");
+               operation = transaction->updates[i];
+       }
+
+       return operation;
+}
+
 int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,
                                       size_t update_idx,
                                       enum ref_transaction_error err,
@@ -2710,7 +2747,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,
                return REF_TRANSACTION_ERROR_GENERIC;
 
        /* Preparing checks before locking references */
-       ret = run_transaction_hook(transaction, "preparing");
+       ret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :
+               run_transaction_hook(transaction, "preparing");
        if (ret) {
                ref_transaction_abort(transaction, err);
                die(_(abort_by_ref_transaction_hook), "preparing");
@@ -2720,7 +2758,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,
        if (ret)
                return ret;
 
-       ret = run_transaction_hook(transaction, "prepared");
+       ret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :
+               run_transaction_hook(transaction, "prepared");
        if (ret) {
                ref_transaction_abort(transaction, err);
                die(_(abort_by_ref_transaction_hook), "prepared");
@@ -2750,7 +2789,8 @@ int ref_transaction_abort(struct ref_transaction *transaction,
                break;
        }
 
-       run_transaction_hook(transaction, "aborted");
+       if (!(transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK))
+               run_transaction_hook(transaction, "aborted");
 
        ref_transaction_free(transaction);
        return ret;
@@ -2781,7 +2821,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
        }
 
        ret = refs->be->transaction_finish(refs, transaction, err);
-       if (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL))
+       if (!ret && !(transaction->flags & (REF_TRANSACTION_FLAG_INITIAL |
+                                        REF_TRANSACTION_FLAG_SKIP_HOOK)))
                run_transaction_hook(transaction, "committed");
        return ret;
 }
@@ -3123,28 +3164,102 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,
        return ret;
 }
 
-int refs_rename_ref(struct ref_store *refs, const char *oldref,
-                   const char *newref, const char *logmsg)
+static int refs_copy_or_rename_ref(struct ref_store *refs, const char *oldref,
+                                  const char *newref, const char *logmsg,
+                                  bool copy)
 {
-       char *msg;
-       int retval;
+       struct ref_transaction *transaction = NULL;
+       struct ref_copy_or_rename_update *operation = NULL;
+       struct ref_update *destination_update;
+       struct object_id old_oid, new_oid;
+       struct strbuf new_target = STRBUF_INIT;
+       struct strbuf err = STRBUF_INIT;
+       char *msg = normalize_reflog_message(logmsg);
+       int old_flags, new_flags = 0, new_exists = 0, ret = 1;
 
-       msg = normalize_reflog_message(logmsg);
-       retval = refs->be->rename_ref(refs, oldref, newref, msg);
+       if (!strcmp(oldref, newref)) {
+               ret = 0;
+               goto out;
+       }
+
+       if (!refs_resolve_ref_unsafe(refs, oldref,
+                                    RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
+                                    &old_oid, &old_flags)) {
+               error("refname %s not found", oldref);
+               goto out;
+       }
+       if (old_flags & REF_ISSYMREF) {
+               error("refname %s is a symbolic ref, %s it is not supported",
+                     oldref, copy ? "copying" : "renaming");
+               goto out;
+       }
+
+       transaction = ref_store_transaction_begin(refs, 0, &err);
+       if (!transaction)
+               goto error;
+       if (!copy && ref_transaction_delete(transaction, oldref, &old_oid, NULL,
+                                           REF_NO_DEREF, msg, &err))
+               goto error;
+
+       if (refs_resolve_ref_unsafe(refs, newref,
+                                   RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
+                                   &new_oid, &new_flags)) {
+               new_exists = 1;
+               if ((new_flags & REF_ISSYMREF) &&
+                   refs_read_symbolic_ref(refs, newref, &new_target) < 0) {
+                       strbuf_addf(&err, "unable to read symbolic ref %s", newref);
+                       goto error;
+               }
+       } else {
+               oidclr(&new_oid, refs->repo->hash_algo);
+       }
+       if (ref_transaction_update(transaction, newref, &old_oid,
+                                  (new_flags & REF_ISSYMREF) ? NULL : &new_oid,
+                                  NULL,
+                                  (new_flags & REF_ISSYMREF) ? new_target.buf : NULL,
+                                  REF_NO_DEREF | REF_SKIP_CREATE_REFLOG,
+                                  NULL, &err))
+               goto error;
+
+       destination_update = transaction->updates[transaction->nr - 1];
+       CALLOC_ARRAY(operation, 1);
+       operation->type = copy ? REF_UPDATE_COPY : REF_UPDATE_RENAME;
+       operation->old_refname = xstrdup(oldref);
+       operation->logmsg = xstrdup(msg);
+       oidcpy(&operation->source_oid, &old_oid);
+       operation->destination_exists = new_exists;
+       if (new_flags & REF_ISSYMREF)
+               operation->destination_target = xstrdup(new_target.buf);
+       else if (operation->destination_exists)
+               oidcpy(&operation->destination_oid, &new_oid);
+       destination_update->copy_or_rename = operation;
+
+       if (ref_transaction_commit(transaction, &err))
+               goto error;
+
+       ret = 0;
+       goto out;
+
+error:
+       error("%s", err.buf);
+out:
+       ref_transaction_free(transaction);
+       strbuf_release(&new_target);
+       strbuf_release(&err);
        free(msg);
-       return retval;
+       return ret;
 }
 
-int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
+int refs_rename_ref(struct ref_store *refs, const char *oldref,
                    const char *newref, const char *logmsg)
 {
-       char *msg;
-       int retval;
+       return refs_copy_or_rename_ref(refs, oldref, newref, logmsg, 0);
+}
 
-       msg = normalize_reflog_message(logmsg);
-       retval = refs->be->copy_ref(refs, oldref, newref, msg);
-       free(msg);
-       return retval;
+int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
+                   const char *newref, const char *logmsg)
+{
+       return refs_copy_or_rename_ref(refs, oldref, newref, logmsg, 1);
 }
 
 const char *ref_update_original_update_refname(struct ref_update *update)
refs/debug.c +0 -25
index 639db0f26..87b84e767 100644
@@ -143,28 +143,6 @@ static int debug_optimize_required(struct ref_store *ref_store,
        return res;
 }
 
-static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,
-                           const char *newref, const char *logmsg)
-{
-       struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-       int res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref,
-                                             logmsg);
-       trace_printf_key(&trace_refs, "rename_ref: %s -> %s \"%s\": %d\n", oldref, newref,
-               logmsg, res);
-       return res;
-}
-
-static int debug_copy_ref(struct ref_store *ref_store, const char *oldref,
-                         const char *newref, const char *logmsg)
-{
-       struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-       int res =
-               drefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg);
-       trace_printf_key(&trace_refs, "copy_ref: %s -> %s \"%s\": %d\n", oldref, newref,
-               logmsg, res);
-       return res;
-}
-
 struct debug_ref_iterator {
        struct ref_iterator base;
        struct ref_iterator *iter;
@@ -453,9 +431,6 @@ struct ref_storage_be refs_be_debug = {
        .optimize = debug_optimize,
        .optimize_required = debug_optimize_required,
 
-       .rename_ref = debug_rename_ref,
-       .copy_ref = debug_copy_ref,
-
        .iterator_begin = debug_ref_iterator_begin,
        .read_raw_ref = debug_read_raw_ref,
        .read_symbolic_ref = debug_read_symbolic_ref,
refs/files-backend.c +266 -31
index 71628550f..c28228116 100644
@@ -1594,6 +1594,7 @@ static int files_optimize_required(struct ref_store *ref_store,
  * live into logs/refs.
  */
 #define TMP_RENAMED_LOG  "refs/.tmp-renamed-log"
+#define TMP_RENAMED_LOG_DESTINATION "refs/.tmp-renamed-log-destination"
 
 struct rename_cb {
        const char *tmp_renamed_log;
@@ -1685,12 +1686,28 @@ static int refs_rename_ref_available(struct ref_store *refs,
        return ok;
 }
 
+struct files_copy_or_rename_transaction_data {
+       struct ref_lock *lock;
+       struct object_id orig_oid;
+       struct object_id destination_oid;
+       char *destination_target;
+       int logmoved;
+       int destination_exists;
+       int destination_log_backed_up;
+};
+
 static int files_copy_or_rename_ref(struct ref_store *ref_store,
-                           const char *oldrefname, const char *newrefname,
-                           const char *logmsg, int copy)
+                                   struct ref_update *update,
+                                   struct ref_transaction *transaction)
 {
        struct files_ref_store *refs =
-               files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
+               files_downcast(ref_store, REF_STORE_WRITE,
+                              "ref_transaction_prepare");
+       struct ref_copy_or_rename_update *operation = update->copy_or_rename;
+       const char *oldrefname = operation->old_refname;
+       const char *newrefname = update->refname;
+       const char *logmsg = operation->logmsg;
+       bool copy = operation->type == REF_UPDATE_COPY;
        struct object_id orig_oid;
        int flag = 0, logmoved = 0;
        struct ref_lock *lock;
@@ -1698,12 +1715,19 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
        struct strbuf sb_oldref = STRBUF_INIT;
        struct strbuf sb_newref = STRBUF_INIT;
        struct strbuf tmp_renamed_log = STRBUF_INIT;
+       struct strbuf tmp_destination_log = STRBUF_INIT;
+       struct strbuf destination_target = STRBUF_INIT;
        int log, ret;
+       int destination_exists = 0, destination_flags = 0;
+       int destination_log_backed_up = 0;
+       struct object_id destination_oid;
+       struct files_copy_or_rename_transaction_data *data;
        struct strbuf err = STRBUF_INIT;
 
        files_reflog_path(refs, &sb_oldref, oldrefname);
        files_reflog_path(refs, &sb_newref, newrefname);
        files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);
+       files_reflog_path(refs, &tmp_destination_log, TMP_RENAMED_LOG_DESTINATION);
 
        log = !lstat(sb_oldref.buf, &loginfo);
        if (log && S_ISLNK(loginfo.st_mode)) {
@@ -1727,11 +1751,67 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
                                    oldrefname);
                goto out;
        }
+       if (!oideq(&orig_oid, &operation->source_oid)) {
+               ret = error("refname %s is at %s but expected %s",
+                           oldrefname, oid_to_hex(&orig_oid),
+                           oid_to_hex(&operation->source_oid));
+               goto out;
+       }
        if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
                ret = 1;
                goto out;
        }
 
+       if (refs_resolve_ref_unsafe(&refs->base, newrefname,
+                                   RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
+                                   &destination_oid, &destination_flags)) {
+               destination_exists = 1;
+               if ((destination_flags & REF_ISSYMREF) &&
+                   refs_read_symbolic_ref(&refs->base, newrefname,
+                                          &destination_target) < 0) {
+                       ret = error("unable to read symbolic ref %s", newrefname);
+                       goto out;
+               }
+       }
+       if (destination_exists != operation->destination_exists) {
+               ret = error("refname %s changed while renaming", newrefname);
+               goto out;
+       }
+       if (destination_exists) {
+               if (destination_flags & REF_ISSYMREF) {
+                       if (!operation->destination_target ||
+                           strcmp(destination_target.buf,
+                                  operation->destination_target)) {
+                               ret = error("refname %s changed while renaming",
+                                           newrefname);
+                               goto out;
+                       }
+               } else if (operation->destination_target ||
+                          !oideq(&destination_oid,
+                                 &operation->destination_oid)) {
+                       ret = error("refname %s changed while renaming", newrefname);
+                       goto out;
+               }
+       }
+
+       if (!lstat(sb_newref.buf, &loginfo)) {
+               if (S_ISLNK(loginfo.st_mode)) {
+                       ret = error("reflog for %s is a symlink", newrefname);
+                       goto out;
+               }
+               if (S_ISREG(loginfo.st_mode)) {
+                       if (copy_file(refs->base.repo, tmp_destination_log.buf,
+                                     sb_newref.buf, 0644)) {
+                               if (errno != EEXIST)
+                                       unlink(tmp_destination_log.buf);
+                               ret = error("unable to back up logfile logs/%s: %s",
+                                           newrefname, strerror(errno));
+                               goto out;
+                       }
+                       destination_log_backed_up = 1;
+               }
+       }
+
        if (!copy && log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
                ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
                            oldrefname, strerror(errno));
@@ -1744,8 +1824,10 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
                goto out;
        }
 
-       if (!copy && refs_delete_ref(&refs->base, logmsg, oldrefname,
-                           &orig_oid, REF_NO_DEREF)) {
+       if (!copy && refs_delete_ref_with_transaction_flags(&refs->base, logmsg,
+                                                        oldrefname, &orig_oid,
+                                                        REF_NO_DEREF,
+                                                        REF_TRANSACTION_FLAG_SKIP_HOOK)) {
                error("unable to delete old %s", oldrefname);
                goto rollback;
        }
@@ -1760,8 +1842,9 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
        if (!copy && refs_resolve_ref_unsafe(&refs->base, newrefname,
                                             RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
                                             NULL, NULL) &&
-           refs_delete_ref(&refs->base, NULL, newrefname,
-                           NULL, REF_NO_DEREF)) {
+           refs_delete_ref_with_transaction_flags(&refs->base, NULL, newrefname,
+                                                    NULL, REF_NO_DEREF,
+                                                    REF_TRANSACTION_FLAG_SKIP_HOOK)) {
                if (errno == EISDIR) {
                        struct strbuf path = STRBUF_INIT;
                        int result;
@@ -1796,13 +1879,25 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
        }
        oidcpy(&lock->old_oid, &orig_oid);
 
-       if (write_ref_to_lockfile(refs, lock, &orig_oid, &err) ||
-           commit_ref_update(refs, lock, &orig_oid, logmsg, 0, &err)) {
+       if (write_ref_to_lockfile(refs, lock, &orig_oid, &err)) {
                error("unable to write current sha1 into %s: %s", newrefname, err.buf);
                strbuf_release(&err);
                goto rollback;
        }
 
+       CALLOC_ARRAY(data, 1);
+       data->lock = lock;
+       oidcpy(&data->orig_oid, &orig_oid);
+       data->logmoved = logmoved;
+       data->destination_exists = destination_exists;
+       data->destination_log_backed_up = destination_log_backed_up;
+       if (destination_exists && !(destination_flags & REF_ISSYMREF))
+               oidcpy(&data->destination_oid, &destination_oid);
+       if (destination_flags & REF_ISSYMREF)
+               data->destination_target = strbuf_detach(&destination_target, NULL);
+       transaction->backend_data = data;
+       transaction->state = REF_TRANSACTION_PREPARED;
+
        ret = 0;
        goto out;
 
@@ -1821,38 +1916,40 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
        }
 
  rollbacklog:
-       if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
-               error("unable to restore logfile %s from %s: %s",
-                       oldrefname, newrefname, strerror(errno));
+       if (logmoved) {
+               if (rename(sb_newref.buf, tmp_renamed_log.buf)) {
+                       error("unable to restore logfile %s from %s: %s",
+                             oldrefname, newrefname, strerror(errno));
+               } else {
+                       try_remove_empty_parents(refs, newrefname,
+                                                REMOVE_EMPTY_PARENTS_REFLOG);
+                       if (rename_tmp_log(refs, oldrefname))
+                               error("unable to restore logfile %s from logs/"
+                                     TMP_RENAMED_LOG ": %s",
+                                     oldrefname, strerror(errno));
+               }
+       }
        if (!logmoved && log &&
            rename(tmp_renamed_log.buf, sb_oldref.buf))
                error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s",
                        oldrefname, strerror(errno));
+       if (destination_log_backed_up &&
+           rename(tmp_destination_log.buf, sb_newref.buf))
+               error("unable to restore logfile %s: %s",
+                     newrefname, strerror(errno));
        ret = 1;
  out:
+       if (ret && destination_log_backed_up)
+               unlink(tmp_destination_log.buf);
        strbuf_release(&sb_newref);
        strbuf_release(&sb_oldref);
        strbuf_release(&tmp_renamed_log);
+       strbuf_release(&tmp_destination_log);
+       strbuf_release(&destination_target);
 
        return ret;
 }
 
-static int files_rename_ref(struct ref_store *ref_store,
-                           const char *oldrefname, const char *newrefname,
-                           const char *logmsg)
-{
-       return files_copy_or_rename_ref(ref_store, oldrefname,
-                                newrefname, logmsg, 0);
-}
-
-static int files_copy_ref(struct ref_store *ref_store,
-                           const char *oldrefname, const char *newrefname,
-                           const char *logmsg)
-{
-       return files_copy_or_rename_ref(ref_store, oldrefname,
-                                newrefname, logmsg, 1);
-}
-
 static int close_ref_gently(struct ref_lock *lock)
 {
        if (close_lock_file_gently(&lock->lk))
@@ -2962,6 +3059,14 @@ static int files_transaction_prepare(struct ref_store *ref_store,
        struct ref_transaction *packed_transaction = NULL;
 
        assert(err);
+       {
+               struct ref_update *operation =
+                       ref_transaction_copy_or_rename_update(transaction);
+
+               if (operation)
+                       return files_copy_or_rename_ref(ref_store, operation,
+                                                       transaction);
+       }
 
        if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
                goto cleanup;
@@ -3318,6 +3423,10 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,
        return ret;
 }
 
+static int files_transaction_abort(struct ref_store *ref_store,
+                                  struct ref_transaction *transaction,
+                                  struct strbuf *err);
+
 static int files_transaction_finish(struct ref_store *ref_store,
                                    struct ref_transaction *transaction,
                                    struct strbuf *err)
@@ -3333,6 +3442,40 @@ static int files_transaction_finish(struct ref_store *ref_store,
 
 
        assert(err);
+       {
+               struct ref_update *update =
+                       ref_transaction_copy_or_rename_update(transaction);
+
+               if (update) {
+                       struct ref_copy_or_rename_update *operation =
+                               update->copy_or_rename;
+                       struct files_copy_or_rename_transaction_data *data =
+                               transaction->backend_data;
+                       int special_ret;
+
+                       special_ret = commit_ref_update(refs, data->lock, &data->orig_oid,
+                                                       operation->logmsg, 0, err);
+                       if (special_ret) {
+                               error("unable to write current sha1 into %s: %s",
+                                     update->refname, err->buf);
+                               data->lock = NULL;
+                               files_transaction_abort(ref_store, transaction, err);
+                               return special_ret;
+                       } else if (data->destination_log_backed_up) {
+                               struct strbuf path = STRBUF_INIT;
+
+                               files_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);
+                               if (unlink(path.buf) < 0 && errno != ENOENT)
+                                       warning_errno("unable to remove '%s'", path.buf);
+                               strbuf_release(&path);
+                       }
+                       free(data->destination_target);
+                       free(data);
+                       transaction->backend_data = NULL;
+                       transaction->state = REF_TRANSACTION_CLOSED;
+                       return special_ret;
+               }
+       }
 
        if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
                return files_transaction_finish_initial(refs, transaction, err);
@@ -3476,11 +3619,105 @@ static int files_transaction_finish(struct ref_store *ref_store,
 
 static int files_transaction_abort(struct ref_store *ref_store,
                                   struct ref_transaction *transaction,
-                                  struct strbuf *err UNUSED)
+                                  struct strbuf *err)
 {
        struct files_ref_store *refs =
                files_downcast(ref_store, 0, "ref_transaction_abort");
 
+       {
+               struct ref_update *update =
+                       ref_transaction_copy_or_rename_update(transaction);
+
+               if (update) {
+                       struct ref_copy_or_rename_update *operation =
+                               update->copy_or_rename;
+                       struct files_copy_or_rename_transaction_data *data =
+                               transaction->backend_data;
+                       struct strbuf new_log = STRBUF_INIT;
+                       struct strbuf destination_log = STRBUF_INIT;
+                       struct strbuf temporary_log = STRBUF_INIT;
+                       struct ref_transaction *restore_transaction = NULL;
+                       struct ref_lock *lock;
+                       int ret = 0;
+
+                       if (data->lock)
+                               unlock_ref(data->lock);
+                       if (operation->type == REF_UPDATE_RENAME) {
+                               lock = lock_ref_oid_basic(refs, operation->old_refname, err);
+                               if (!lock ||
+                                   write_ref_to_lockfile(refs, lock, &data->orig_oid, err) ||
+                                   commit_ref_update(refs, lock, &data->orig_oid, NULL,
+                                                     REF_SKIP_CREATE_REFLOG, err))
+                                       ret = -1;
+                       }
+
+                       if (data->logmoved) {
+                               files_reflog_path(refs, &new_log, update->refname);
+                               if (operation->type == REF_UPDATE_RENAME) {
+                                       files_reflog_path(refs, &temporary_log, TMP_RENAMED_LOG);
+                                       if (rename(new_log.buf, temporary_log.buf) < 0) {
+                                               strbuf_addf(err, "unable to restore logfile %s: %s",
+                                                           operation->old_refname, strerror(errno));
+                                               ret = -1;
+                                       } else {
+                                               try_remove_empty_parents(refs,
+                                                                        update->refname,
+                                                                        REMOVE_EMPTY_PARENTS_REFLOG);
+                                               if (rename_tmp_log(refs,
+                                                                  operation->old_refname)) {
+                                                       strbuf_addf(err, "unable to restore logfile %s: %s",
+                                                                   operation->old_refname,
+                                                                   strerror(errno));
+                                                       ret = -1;
+                                               }
+                                       }
+                               } else if (unlink(new_log.buf) < 0 && errno != ENOENT) {
+                                       strbuf_addf(err, "unable to remove logfile %s: %s",
+                                                   update->refname, strerror(errno));
+                                       ret = -1;
+                               }
+                       }
+                       if (data->destination_log_backed_up) {
+                               files_reflog_path(refs, &destination_log,
+                                                 TMP_RENAMED_LOG_DESTINATION);
+                               if (rename(destination_log.buf, new_log.buf) < 0) {
+                                       strbuf_addf(err, "unable to restore logfile %s: %s",
+                                                   update->refname, strerror(errno));
+                                       ret = -1;
+                               }
+                       }
+
+                       if (operation->type == REF_UPDATE_RENAME &&
+                           data->destination_exists) {
+                               restore_transaction = ref_store_transaction_begin(
+                                       &refs->base, REF_TRANSACTION_FLAG_SKIP_HOOK, err);
+                               if (!restore_transaction ||
+                                   ref_transaction_update(restore_transaction,
+                                                          update->refname,
+                                                          data->destination_target ? NULL :
+                                                                                     &data->destination_oid,
+                                                          NULL,
+                                                          data->destination_target,
+                                                          NULL,
+                                                          REF_NO_DEREF |
+                                                                  REF_SKIP_CREATE_REFLOG,
+                                                          NULL, err) ||
+                                   ref_transaction_commit(restore_transaction, err))
+                                       ret = -1;
+                               ref_transaction_free(restore_transaction);
+                       }
+
+                       strbuf_release(&destination_log);
+                       strbuf_release(&temporary_log);
+                       strbuf_release(&new_log);
+                       free(data->destination_target);
+                       free(data);
+                       transaction->backend_data = NULL;
+                       transaction->state = REF_TRANSACTION_CLOSED;
+                       return ret;
+               }
+       }
+
        files_transaction_cleanup(refs, transaction);
        return 0;
 }
@@ -4095,8 +4332,6 @@ struct ref_storage_be refs_be_files = {
 
        .optimize = files_optimize,
        .optimize_required = files_optimize_required,
-       .rename_ref = files_rename_ref,
-       .copy_ref = files_copy_ref,
 
        .iterator_begin = files_ref_iterator_begin,
        .read_raw_ref = files_read_raw_ref,
refs/packed-backend.c +0 -2
index a73fc6aca..364a91291 100644
@@ -2164,8 +2164,6 @@ struct ref_storage_be refs_be_packed = {
        .optimize = packed_optimize,
        .optimize_required = packed_optimize_required,
 
-       .rename_ref = NULL,
-       .copy_ref = NULL,
 
        .iterator_begin = packed_ref_iterator_begin,
        .read_raw_ref = packed_read_raw_ref,
refs/refs-internal.h +37 -10
index c3ac7b556..5d4dc0171 100644
@@ -155,9 +155,33 @@ struct ref_update {
         */
        struct ref_update *parent_update;
 
+       /*
+        * Copy and rename operations require backend-specific handling while
+        * still exposing their logical updates to transaction hooks. Keep that
+        * state on the destination update so it composes with other updates in
+        * the transaction instead of making copy or rename a transaction-wide
+        * property.
+        */
+       struct ref_copy_or_rename_update *copy_or_rename;
+
        const char refname[FLEX_ARRAY];
 };
 
+enum ref_copy_or_rename_type {
+       REF_UPDATE_RENAME,
+       REF_UPDATE_COPY,
+};
+
+struct ref_copy_or_rename_update {
+       enum ref_copy_or_rename_type type;
+       char *old_refname;
+       char *logmsg;
+       struct object_id source_oid;
+       struct object_id destination_oid;
+       char *destination_target;
+       unsigned int destination_exists:1;
+};
+
 int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
                      struct object_id *oid, struct strbuf *referent,
                      unsigned int *type, int *failure_errno);
@@ -187,6 +211,13 @@ struct ref_update *ref_transaction_add_update(
                const char *committer_info,
                const char *msg);
 
+int refs_delete_ref_with_transaction_flags(struct ref_store *refs,
+                                          const char *msg,
+                                          const char *refname,
+                                          const struct object_id *old_oid,
+                                          unsigned int flags,
+                                          unsigned int transaction_flags);
+
 /*
  * Transaction states.
  *
@@ -242,6 +273,12 @@ struct ref_transaction {
        uint64_t max_index;
 };
 
+/* Suppress hooks for a transaction nested inside another refs operation. */
+#define REF_TRANSACTION_FLAG_SKIP_HOOK (1 << 2)
+
+struct ref_update *ref_transaction_copy_or_rename_update(
+       struct ref_transaction *transaction);
+
 /*
  * Check for entries in extras that are within the specified
  * directory, where dirname is a reference directory name including
@@ -451,13 +488,6 @@ typedef int optimize_required_fn(struct ref_store *ref_store,
                                 struct refs_optimize_opts *opts,
                                 bool *required);
 
-typedef int rename_ref_fn(struct ref_store *ref_store,
-                         const char *oldref, const char *newref,
-                         const char *logmsg);
-typedef int copy_ref_fn(struct ref_store *ref_store,
-                         const char *oldref, const char *newref,
-                         const char *logmsg);
-
 /*
  * Iterate over the references in `ref_store` whose names start with
  * `prefix`. `prefix` is matched as a literal string, without regard
@@ -577,9 +607,6 @@ struct ref_storage_be {
 
        optimize_fn *optimize;
        optimize_required_fn *optimize_required;
-       rename_ref_fn *rename_ref;
-       copy_ref_fn *copy_ref;
-
        ref_iterator_begin_fn *iterator_begin;
        read_raw_ref_fn *read_raw_ref;
 
refs/reftable-backend.c +149 -48
index 10db03991..589fcc998 100644
@@ -953,6 +953,14 @@ struct reftable_transaction_data {
        size_t args_nr, args_alloc;
 };
 
+struct reftable_copy_or_rename_transaction_data {
+       struct reftable_addition *addition;
+};
+
+static int reftable_be_copy_or_rename_prepare(struct ref_store *ref_store,
+                                              struct ref_transaction *transaction,
+                                              struct strbuf *err);
+
 static void free_transaction_data(struct reftable_transaction_data *tx_data)
 {
        if (!tx_data)
@@ -1326,6 +1334,10 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
        size_t i;
        int ret;
 
+       if (ref_transaction_copy_or_rename_update(transaction))
+               return reftable_be_copy_or_rename_prepare(ref_store, transaction,
+                                                          err);
+
        ret = refs->err;
        if (ret < 0)
                goto done;
@@ -1419,7 +1431,20 @@ static int reftable_be_transaction_abort(struct ref_store *ref_store UNUSED,
                                         struct ref_transaction *transaction,
                                         struct strbuf *err UNUSED)
 {
-       struct reftable_transaction_data *tx_data = transaction->backend_data;
+       struct reftable_transaction_data *tx_data;
+
+       if (ref_transaction_copy_or_rename_update(transaction)) {
+               struct reftable_copy_or_rename_transaction_data *data =
+                       transaction->backend_data;
+
+               reftable_addition_destroy(data->addition);
+               free(data);
+               transaction->backend_data = NULL;
+               transaction->state = REF_TRANSACTION_CLOSED;
+               return 0;
+       }
+
+       tx_data = transaction->backend_data;
        free_transaction_data(tx_data);
        transaction->state = REF_TRANSACTION_CLOSED;
        return 0;
@@ -1667,9 +1692,28 @@ static int reftable_be_transaction_finish(struct ref_store *ref_store UNUSED,
                                          struct ref_transaction *transaction,
                                          struct strbuf *err)
 {
-       struct reftable_transaction_data *tx_data = transaction->backend_data;
+       struct reftable_transaction_data *tx_data;
        int ret = 0;
 
+       if (ref_transaction_copy_or_rename_update(transaction)) {
+               struct reftable_copy_or_rename_transaction_data *data =
+                       transaction->backend_data;
+               int special_ret = reftable_addition_commit(data->addition);
+
+               reftable_addition_destroy(data->addition);
+               free(data);
+               transaction->backend_data = NULL;
+               transaction->state = REF_TRANSACTION_CLOSED;
+               if (special_ret < 0) {
+                       strbuf_addf(err, _("reftable: transaction failure: %s"),
+                                   reftable_error_str(special_ret));
+                       return -1;
+               }
+               return 0;
+       }
+
+       tx_data = transaction->backend_data;
+
        for (size_t i = 0; i < tx_data->args_nr; i++) {
                tx_data->args[i].max_index = transaction->max_index;
 
@@ -1764,17 +1808,20 @@ struct write_create_symref_arg {
 struct write_copy_arg {
        struct reftable_ref_store *refs;
        struct reftable_backend *be;
+       struct strbuf *err;
        const char *oldname;
        const char *newname;
        const char *logmsg;
        int delete_old;
+       struct ref_copy_or_rename_update *operation;
 };
 
 static int write_copy_table(struct reftable_writer *writer, void *cb_data)
 {
        struct write_copy_arg *arg = cb_data;
        uint64_t deletion_ts, creation_ts;
-       struct reftable_ref_record old_ref = {0}, refs[2] = {0};
+       struct reftable_ref_record old_ref = {0}, destination_ref = {0};
+       struct reftable_ref_record refs[2] = {0};
        struct reftable_log_record old_log = {0}, *logs = NULL;
        struct reftable_iterator it = {0};
        struct string_list skip = STRING_LIST_INIT_NODUP;
@@ -1789,14 +1836,75 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)
                BUG("failed splitting committer info");
 
        if (reftable_stack_read_ref(arg->be->stack, arg->oldname, &old_ref)) {
-               ret = error(_("refname %s not found"), arg->oldname);
+               strbuf_addf(arg->err, _("refname %s not found"), arg->oldname);
+               ret = -1;
                goto done;
        }
        if (old_ref.value_type == REFTABLE_REF_SYMREF) {
-               ret = error(_("refname %s is a symbolic ref, copying it is not supported"),
+               strbuf_addf(arg->err,
+                           _("refname %s is a symbolic ref, copying it is not supported"),
                            arg->oldname);
+               ret = -1;
                goto done;
        }
+       if (arg->operation) {
+               struct object_id oid;
+
+               if (old_ref.value_type == REFTABLE_REF_VAL2)
+                       oidread(&oid, old_ref.value.val2.value,
+                               arg->refs->base.repo->hash_algo);
+               else
+                       oidread(&oid, old_ref.value.val1,
+                               arg->refs->base.repo->hash_algo);
+               if (!oideq(&oid, &arg->operation->source_oid)) {
+                       strbuf_addf(arg->err,
+                                   _("refname %s is at %s but expected %s"),
+                                   arg->oldname, oid_to_hex(&oid),
+                                   oid_to_hex(&arg->operation->source_oid));
+                       ret = -1;
+                       goto done;
+               }
+
+               ret = reftable_stack_read_ref(arg->be->stack, arg->newname,
+                                             &destination_ref);
+               if (ret < 0)
+                       goto done;
+               if (arg->operation->destination_exists != !ret) {
+                       strbuf_addf(arg->err,
+                                   _("refname %s changed while renaming"),
+                                   arg->newname);
+                       ret = -1;
+                       goto done;
+               }
+               if (!ret) {
+                       if (destination_ref.value_type == REFTABLE_REF_SYMREF) {
+                               if (!arg->operation->destination_target ||
+                                   strcmp(destination_ref.value.symref,
+                                          arg->operation->destination_target)) {
+                                       strbuf_addf(arg->err,
+                                                   _("refname %s changed while renaming"),
+                                                   arg->newname);
+                                       ret = -1;
+                                       goto done;
+                               }
+                       } else {
+                               if (destination_ref.value_type == REFTABLE_REF_VAL2)
+                                       oidread(&oid, destination_ref.value.val2.value,
+                                               arg->refs->base.repo->hash_algo);
+                               else
+                                       oidread(&oid, destination_ref.value.val1,
+                                               arg->refs->base.repo->hash_algo);
+                               if (arg->operation->destination_target ||
+                                   !oideq(&oid, &arg->operation->destination_oid)) {
+                                       strbuf_addf(arg->err,
+                                                   _("refname %s changed while renaming"),
+                                                   arg->newname);
+                                       ret = -1;
+                                       goto done;
+                               }
+                       }
+               }
+       }
 
        /*
         * There's nothing to do in case the old and new name are the same, so
@@ -1815,7 +1923,7 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)
        ret = refs_verify_refname_available(&arg->refs->base, arg->newname,
                                            NULL, &skip, 0, &errbuf);
        if (ret < 0) {
-               error("%s", errbuf.buf);
+               strbuf_addbuf(arg->err, &errbuf);
                goto done;
        }
 
@@ -1980,68 +2088,63 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data)
        for (i = 0; i < ARRAY_SIZE(refs); i++)
                reftable_ref_record_release(&refs[i]);
        reftable_ref_record_release(&old_ref);
+       reftable_ref_record_release(&destination_ref);
        reftable_log_record_release(&old_log);
        return ret;
 }
 
-static int reftable_be_rename_ref(struct ref_store *ref_store,
-                                 const char *oldrefname,
-                                 const char *newrefname,
-                                 const char *logmsg)
+static int reftable_be_copy_or_rename_prepare(struct ref_store *ref_store,
+                                              struct ref_transaction *transaction,
+                                              struct strbuf *err)
 {
        struct reftable_ref_store *refs =
-               reftable_be_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
+               reftable_be_downcast(ref_store, REF_STORE_WRITE,
+                                    "ref_transaction_prepare");
+       struct reftable_copy_or_rename_transaction_data *data = NULL;
+       struct ref_update *update =
+               ref_transaction_copy_or_rename_update(transaction);
+       struct ref_copy_or_rename_update *operation = update->copy_or_rename;
        struct write_copy_arg arg = {
                .refs = refs,
-               .oldname = oldrefname,
-               .newname = newrefname,
-               .logmsg = logmsg,
-               .delete_old = 1,
+               .err = err,
+               .oldname = operation->old_refname,
+               .newname = update->refname,
+               .logmsg = operation->logmsg,
+               .delete_old = operation->type == REF_UPDATE_RENAME,
+               .operation = operation,
        };
        int ret;
 
+       CALLOC_ARRAY(data, 1);
        ret = refs->err;
        if (ret < 0)
                goto done;
-
-       ret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);
+       ret = backend_for(&arg.be, refs, update->refname,
+                         &arg.newname, 1);
        if (ret)
                goto done;
-       ret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,
-                                &reftable_be_write_options(refs)->opts);
-
-done:
-       assert(ret != REFTABLE_API_ERROR);
-       return ret;
-}
-
-static int reftable_be_copy_ref(struct ref_store *ref_store,
-                               const char *oldrefname,
-                               const char *newrefname,
-                               const char *logmsg)
-{
-       struct reftable_ref_store *refs =
-               reftable_be_downcast(ref_store, REF_STORE_WRITE, "copy_ref");
-       struct write_copy_arg arg = {
-               .refs = refs,
-               .oldname = oldrefname,
-               .newname = newrefname,
-               .logmsg = logmsg,
-       };
-       int ret;
-
-       ret = refs->err;
-       if (ret < 0)
+       ret = reftable_stack_addition_new(&data->addition, arg.be->stack,
+                                         &reftable_be_write_options(refs)->opts);
+       if (ret)
                goto done;
-
-       ret = backend_for(&arg.be, refs, newrefname, &newrefname, 1);
+       ret = reftable_addition_add(data->addition, &write_copy_table, &arg);
        if (ret)
                goto done;
-       ret = reftable_stack_add(arg.be->stack, &write_copy_table, &arg,
-                                &reftable_be_write_options(refs)->opts);
+
+       transaction->backend_data = data;
+       transaction->state = REF_TRANSACTION_PREPARED;
+       return 0;
 
 done:
        assert(ret != REFTABLE_API_ERROR);
+       if (data) {
+               reftable_addition_destroy(data->addition);
+               free(data);
+       }
+       transaction->state = REF_TRANSACTION_CLOSED;
+       if (ret && !err->len)
+               strbuf_addf(err, _("reftable: transaction prepare: %s"),
+                           reftable_error_str(ret));
        return ret;
 }
 
@@ -2872,8 +2975,6 @@ struct ref_storage_be refs_be_reftable = {
        .optimize = reftable_be_optimize,
        .optimize_required = reftable_be_optimize_required,
 
-       .rename_ref = reftable_be_rename_ref,
-       .copy_ref = reftable_be_copy_ref,
 
        .iterator_begin = reftable_be_iterator_begin,
        .read_raw_ref = reftable_be_read_raw_ref,
t/t1416-ref-transaction-hooks.sh +142 -1
index 4fe9d9b23..116b2ff07 100755
@@ -93,6 +93,148 @@ test_expect_success 'hook gets all queued updates in committed state' '
        test_cmp expect actual
 '
 
+test_expect_success 'hook gets both updates when renaming a branch' '
+       test_when_finished "rm -f actual" &&
+       git branch old PRE &&
+       test_hook reference-transaction <<-\EOF &&
+               echo "$1" >>actual &&
+               cat >>actual
+       EOF
+       cat >expect <<-EOF &&
+       preparing
+       $PRE_OID $ZERO_OID refs/heads/old
+       $ZERO_OID $PRE_OID refs/heads/new
+       prepared
+       $PRE_OID $ZERO_OID refs/heads/old
+       $ZERO_OID $PRE_OID refs/heads/new
+       committed
+       $PRE_OID $ZERO_OID refs/heads/old
+       $ZERO_OID $PRE_OID refs/heads/new
+       EOF
+       git branch -m old new &&
+       test_cmp expect actual &&
+       test_must_fail git rev-parse --verify refs/heads/old &&
+       test_cmp_rev PRE refs/heads/new
+'
+
+test_expect_success 'hook gets destination update when copying a branch' '
+       test_when_finished "rm -f actual" &&
+       git branch copy-source PRE &&
+       test_hook reference-transaction <<-\EOF &&
+               echo "$1" >>actual &&
+               cat >>actual
+       EOF
+       cat >expect <<-EOF &&
+       preparing
+       $ZERO_OID $PRE_OID refs/heads/copy-destination
+       prepared
+       $ZERO_OID $PRE_OID refs/heads/copy-destination
+       committed
+       $ZERO_OID $PRE_OID refs/heads/copy-destination
+       EOF
+       git branch -c copy-source copy-destination &&
+       test_cmp expect actual &&
+       test_cmp_rev PRE refs/heads/copy-source &&
+       test_cmp_rev PRE refs/heads/copy-destination
+'
+
+test_expect_success 'hook gets overwritten values for forced rename and copy' '
+       git branch force-old PRE &&
+       git branch force-new POST &&
+       git branch force-copy-source PRE &&
+       git branch force-copy-destination POST &&
+       test_hook reference-transaction <<-\EOF &&
+               if test "$1" = committed
+               then
+                       cat >>actual
+               fi
+       EOF
+       git branch -M force-old force-new &&
+       git branch -C force-copy-source force-copy-destination &&
+       cat >expect <<-EOF &&
+       $PRE_OID $ZERO_OID refs/heads/force-old
+       $POST_OID $PRE_OID refs/heads/force-new
+       $POST_OID $PRE_OID refs/heads/force-copy-destination
+       EOF
+       test_cmp expect actual
+'
+
+test_expect_success 'hook can abort a branch rename after preparation' '
+       git branch abort-old PRE &&
+       git branch abort-new POST &&
+       git reflog show --format=%gs abort-old >old-log &&
+       git reflog show --format=%gs abort-new >new-log &&
+       test_hook reference-transaction <<-\EOF &&
+               test "$1" != prepared
+       EOF
+       test_must_fail git branch -M abort-old abort-new &&
+       test_cmp_rev PRE refs/heads/abort-old &&
+       test_cmp_rev POST refs/heads/abort-new &&
+       git reflog show --format=%gs abort-old >old-log-after &&
+       git reflog show --format=%gs abort-new >new-log-after &&
+       test_cmp old-log old-log-after &&
+       test_cmp new-log new-log-after
+'
+
+test_expect_success 'hook can abort a D/F branch rename after preparation' '
+       git branch df-old PRE &&
+       git reflog show --format=%gs df-old >df-log &&
+       test_hook reference-transaction <<-\EOF &&
+               test "$1" != prepared
+       EOF
+       test_must_fail git branch -m df-old df-old/child &&
+       test_cmp_rev PRE refs/heads/df-old &&
+       test_must_fail git rev-parse --verify refs/heads/df-old/child &&
+       git reflog show --format=%gs df-old >df-log-after &&
+       test_cmp df-log df-log-after
+'
+
+test_expect_success 'hook can abort a reverse D/F rename after preparation' '
+       git branch reverse/old PRE &&
+       git reflog show --format=%gs reverse/old >reverse-log &&
+       test_hook reference-transaction <<-\EOF &&
+               test "$1" != prepared
+       EOF
+       test_must_fail git branch -m reverse/old reverse &&
+       test_cmp_rev PRE refs/heads/reverse/old &&
+       test_must_fail git rev-parse --verify refs/heads/reverse &&
+       git reflog show --format=%gs reverse/old >reverse-log-after &&
+       test_cmp reverse-log reverse-log-after
+'
+
+test_expect_success 'hook can abort a forced branch copy after preparation' '
+       git branch copy-abort-old PRE &&
+       git branch copy-abort-new POST &&
+       git reflog show --format=%gs copy-abort-old >copy-old-log &&
+       git reflog show --format=%gs copy-abort-new >copy-new-log &&
+       test_hook reference-transaction <<-\EOF &&
+               test "$1" != prepared
+       EOF
+       test_must_fail git branch -C copy-abort-old copy-abort-new &&
+       test_cmp_rev PRE refs/heads/copy-abort-old &&
+       test_cmp_rev POST refs/heads/copy-abort-new &&
+       git reflog show --format=%gs copy-abort-old >copy-old-log-after &&
+       git reflog show --format=%gs copy-abort-new >copy-new-log-after &&
+       test_cmp copy-old-log copy-old-log-after &&
+       test_cmp copy-new-log copy-new-log-after
+'
+
+test_expect_success 'branch rename detects an update during preparing hook' '
+       git branch race-old PRE &&
+       git branch race-new POST &&
+       test_hook reference-transaction <<-\EOF &&
+               marker=$(git rev-parse --git-path rename-race-once)
+               if test "$1" = preparing && test ! -e "$marker"
+               then
+                       >"$marker" &&
+                       git update-ref refs/heads/race-old POST
+               fi
+       EOF
+       test_must_fail git branch -M race-old race-new &&
+       test_cmp_rev POST refs/heads/race-old &&
+       test_cmp_rev POST refs/heads/race-new
+'
+
 test_expect_success 'hook gets all queued updates in aborted state' '
        test_when_finished "rm actual" &&
        git reset --hard PRE &&
--

2.39.3 (Apple Git-146)

#8 lore raw

Re: [PATCH v2] refs: run copy and rename through transactions

In reply to Maciej Ciemborowicz
Quoted 9 lines · Maciej Ciemborowicz

Reference copy and rename operations bypass the transaction API. Consequently, the reference-transaction hook sees only the source deletion with the files backend and no useful update with the reftable backend.

Represent both operations as reference transactions containing their logical updates. A rename is a deletion of the old reference and creation of the new reference in the same transaction. Attach operation-specific state to the destination update instead of making copy or rename a property of the entire transaction.

Sorry, but what does this last sentence mean? What is the consequence of it?

Quoted 5 lines

Retain backend-specific reflog handling: the files backend stages its existing rename procedure across prepare, finish and abort, while reftable stages an addition while holding the stack lock. Suppress hooks for the files backend's nested deletion transactions so that callers observe one logical transaction.

The fact that we retain the backend-specific logic is not really interesting by itself. The way more interesting question is _why_ we retain it. Or asked differently, why can't we make this whole mechanism completely agnostic of the backend and implement this via pure transactions?

Quoted 5 lines

Record and verify the source and destination values after taking backend locks. This rejects concurrent changes instead of applying a rename or copy that differs from the payload shown to the preparing hook. Preserve D/F renames and restore overwritten references and reflogs when a prepared hook rejects the operation.

Is this new behaviour? Is this retaining old behaviour? I have no clue.

Quoted 2 lines

Add tests covering rename, copy, forced updates, both directions of D/F conflicts, concurrent updates and prepared-hook rollback.

This sentence doesn't really add much value to the message.

How does all of this impact performance?

Quoted 10 lines
refs.c +2 -0
index 92d5df5b7..f036ae4b9 100644
@@ -1027,6 +1029,15 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
 	return 0;
 }
 
+int refs_delete_ref(struct ref_store *refs, const char *msg,
+		    const char *refname,

refs.c int refs_delete_ref(struct ref_store *refs, const char *msg, · line 1043

Quoted 10 lines
refs.c +7 -0
+		    const struct object_id *old_oid,
+		    unsigned int flags)
+{
+	return refs_delete_ref_with_transaction_flags(refs, msg, refname,
+						      old_oid, flags, 0);
+}
+
 static void copy_reflog_msg(struct strbuf *sb, const char *msg)
 {
 	char c;

Refactorings like these could easily go into a separate commit to make this easier to review.

refs.c int ref_transaction_prepare(struct ref_transaction *transaction, · line 2754

Quoted 10 lines
refs.c +2 -1
@@ -2710,7 +2747,8 @@ int ref_transaction_prepare(struct ref_transaction *transaction,
 		return REF_TRANSACTION_ERROR_GENERIC;
 
 	/* Preparing checks before locking references */
-	ret = run_transaction_hook(transaction, "preparing");
+	ret = transaction->flags & REF_TRANSACTION_FLAG_SKIP_HOOK ? 0 :
+		run_transaction_hook(transaction, "preparing");
 	if (ret) {
 		ref_transaction_abort(transaction, err);
 		die(_(abort_by_ref_transaction_hook), "preparing");

Instead of teaching every site to conditionally call run_transaction_hook() only when the flag is not set, can't we adapt the function itself to skip?

In any case, this is another change that could easily be split out into a separate commit.

Quoted 9 lines
refs/files-backend.c +1 -0
index 71628550f..c28228116 100644
@@ -2962,6 +3059,14 @@ static int files_transaction_prepare(struct ref_store *ref_store,
 	struct ref_transaction *packed_transaction = NULL;
 
 	assert(err);
+	{

refs/files-backend.c static int files_transaction_prepare(struct ref_store *ref_store, · line 3072

Quoted 10 lines
refs/files-backend.c +7 -0
+		struct ref_update *operation =
+			ref_transaction_copy_or_rename_update(transaction);
+
+		if (operation)
+			return files_copy_or_rename_ref(ref_store, operation,
+							transaction);
+	}
 
 	if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
 		goto cleanup;

I know this is a construct that AI loves, but that's not following our coding style.

Quoted 31 lines
refs/files-backend.c +27 -0
@@ -3333,6 +3442,40 @@ static int files_transaction_finish(struct ref_store *ref_store,
 
 
 	assert(err);
+	{
+		struct ref_update *update =
+			ref_transaction_copy_or_rename_update(transaction);
+
+		if (update) {
+			struct ref_copy_or_rename_update *operation =
+				update->copy_or_rename;
+			struct files_copy_or_rename_transaction_data *data =
+				transaction->backend_data;
+			int special_ret;
+
+			special_ret = commit_ref_update(refs, data->lock, &data->orig_oid,
+							operation->logmsg, 0, err);
+			if (special_ret) {
+				error("unable to write current sha1 into %s: %s",
+				      update->refname, err->buf);
+				data->lock = NULL;
+				files_transaction_abort(ref_store, transaction, err);
+				return special_ret;
+			} else if (data->destination_log_backed_up) {
+				struct strbuf path = STRBUF_INIT;
+
+				files_reflog_path(refs, &path, TMP_RENAMED_LOG_DESTINATION);
+				if (unlink(path.buf) < 0 && errno != ENOENT)
+					warning_errno("unable to remove '%s'", path.buf);
+				strbuf_release(&path);
+			}

refs/files-backend.c static int files_transaction_finish(struct ref_store *ref_store, · line 3481

Quoted 10 lines
refs/files-backend.c +7 -0
+			free(data->destination_target);
+			free(data);
+			transaction->backend_data = NULL;
+			transaction->state = REF_TRANSACTION_CLOSED;
+			return special_ret;
+		}
+	}
 
 	if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
 		return files_transaction_finish_initial(refs, transaction, err);

Yeah...

refs/files-backend.c static int files_transaction_finish(struct ref_store *ref_store, · line 3629

Quoted 13 lines
refs/files-backend.c +4 -1
@@ -3476,11 +3619,105 @@ static int files_transaction_finish(struct ref_store *ref_store,
 
 static int files_transaction_abort(struct ref_store *ref_store,
 				   struct ref_transaction *transaction,
-				   struct strbuf *err UNUSED)
+				   struct strbuf *err)
 {
 	struct files_ref_store *refs =
 		files_downcast(ref_store, 0, "ref_transaction_abort");
 
+	{
+		struct ref_update *update =
+			ref_transaction_copy_or_rename_update(transaction);

... really?

Sorry, but I'm going to stop reading here. This is not in a state that is reviewable and has way too much stuff that is obviously generated by an AI without much thought being put into it by the author. I don't want to invest my time into a topic where the author has obviously not spent their time thinking about it, either.

Patrick

#9 lore raw

Re: [PATCH v2] refs: run copy and rename through transactions

Quoted 2 lines · Patrick Steinhardt

Sorry, but what does this last sentence mean? What is the consequence of it?

The intent was to address Junio's comment about making copy/rename a property of the whole ref_transaction. In v2 the extra state is attached to the destination ref_update instead.

Quoted 2 lines

why can't we make this whole mechanism completely agnostic of the backend and implement this via pure transactions?

The part I was trying to preserve is the existing reflog semantics. A normal ref transaction can express the logical ref updates. In example deleting the old ref and creating/updating the destination. But branch rename/copy also moves or copies the existing reflog history. For the files backend that currently involves filesystem-level reflog rename/copy and D/F handling, while reftable represents the same operation differently. So my assumption was that the logical ref updates could go through the generic transaction API, while the reflog-history operation would remain backend-specific.

Quoted 1 line

Is this new behaviour? Is this retaining old behaviour?

The source/destination revalidation is new validation required by introducing the preparing hook before the backend locks are taken. The hook can itself change one of the refs. Without revalidation, the hook payload could describe one state while the rename/copy later operates on another state. The intention is therefore to reject an operation when the state observed by the preparing hook is no longer the state being committed.

Quoted 1 line

How does all of this impact performance?

Enabling reference-transaction for rename/copy naturally adds the cost of invoking the hook when one is installed. I measured git branch -m and git branch -c. Each result is the median of five blocks of 40 commands per version:

Hook Before After Change

files     branch -m       no      5.089 ms   5.234 ms   +3.8%
files     branch -m       yes    12.608 ms  11.134 ms  -10.8%
files     branch -c       no      4.753 ms   4.916 ms   +3.4%
files     branch -c       yes     5.509 ms  12.732 ms +137.7%
reftable  branch -m       no      6.478 ms   6.998 ms   +7.8%
reftable  branch -m       yes     5.918 ms  13.229 ms +114.6%
reftable  branch -c       no      5.620 ms   6.368 ms  +10.9%
reftable  branch -c       yes     6.261 ms  12.354 ms  +97.0%
Quoted 5 lines

Sorry, but I'm going to stop reading here. This is not in a state that is reviewable and has way too much stuff that is obviously generated by an AI without much thought being put into it by the author. I don't want to invest my time into a topic where the author has obviously not spent their time thinking about it, either.

I'm really sorry to hear that. Yes, the patches I prepared were AI-assisted, but I do feel that I understand what I am doing. I would appreciate some understanding, though, as I do not work with C on a daily basis. The bug report and my attempt to fix it came from the fact that I am working on a Ruby gem for per-branch and per-worktree containerization. That is why I had to write git-hooks-ext, which is how I ended up running into this bug in the first place.

I am not insisting that my patch should be merged. I simply thought that submitting a patch might help get the bug fixed faster, and getting the bug fixed is what I care about most. Karthik Nayak offered to help fix it, so perhaps it would be better for someone who works with C on a daily basis to take it over.

I can, of course, also prepare a v3, split it into more commits, and explain my reasoning more clearly. But I cannot guarantee that it will meet your standards, simply because I am not yet familiar with them.

#10 lore raw

Re: [PATCH v2] refs: run copy and rename through transactions

Quoted 4 lines · Maciej Ciemborowicz
Quoted 2 lines · Patrick Steinhardt

why can't we make this whole mechanism completely agnostic of the backend and implement this via pure transactions?

Quoted 9 lines

The part I was trying to preserve is the existing reflog semantics. A normal ref transaction can express the logical ref updates. In example deleting the old ref and creating/updating the destination. But branch rename/copy also moves or copies the existing reflog history. For the files backend that currently involves filesystem-level reflog rename/copy and D/F handling, while reftable represents the same operation differently. So my assumption was that the logical ref updates could go through the generic transaction API, while the reflog-history operation would remain backend-specific.

Yes, the reflog semantics should of course stay the same. But nowadays, this would also be achievable with only backend-agnostic logic as the reference transactions have learned to write many reflog entries for a single reference. This was added back when we introduced the migration logic to convert between two different backends.

Now there's potentially two caveats:

- I don't think we have a way to delete many old reflog entries yet.

- There may be a significant impact on performance.

The question thus is whether we can avoid or fix those caveats somehow and thus arrive at a more future-proof mechanism.

Quoted 9 lines
Quoted 1 line

Is this new behaviour? Is this retaining old behaviour?

The source/destination revalidation is new validation required by introducing the preparing hook before the backend locks are taken. The hook can itself change one of the refs. Without revalidation, the hook payload could describe one state while the rename/copy later operates on another state. The intention is therefore to reject an operation when the state observed by the preparing hook is no longer the state being committed.

I don't feel like that's sensible. The "preparing" hook is explicitly run before we perform locking and is documented as such. So it is fully expected that the on-disk state may still change between executing this and the "prepared" phase. It is the responsibility of the hook author to handle such cases, we shouldn't do this ourselves as we're now starting to assume semantics of the hook itself.

Quoted 20 lines
Quoted 5 lines

Sorry, but I'm going to stop reading here. This is not in a state that is reviewable and has way too much stuff that is obviously generated by an AI without much thought being put into it by the author. I don't want to invest my time into a topic where the author has obviously not spent their time thinking about it, either.

I'm really sorry to hear that. Yes, the patches I prepared were AI-assisted, but I do feel that I understand what I am doing. I would appreciate some understanding, though, as I do not work with C on a daily basis. The bug report and my attempt to fix it came from the fact that I am working on a Ruby gem for per-branch and per-worktree containerization. That is why I had to write git-hooks-ext, which is how I ended up running into this bug in the first place.

I am not insisting that my patch should be merged. I simply thought that submitting a patch might help get the bug fixed faster, and getting the bug fixed is what I care about most. Karthik Nayak offered to help fix it, so perhaps it would be better for someone who works with C on a daily basis to take it over.

Quoted 3 lines

I can, of course, also prepare a v3, split it into more commits, and explain my reasoning more clearly. But I cannot guarantee that it will meet your standards, simply because I am not yet familiar with them.

I'd suggest to iterate then. In the current version this patch is not in a shape that is ready for review. The patch needs to be split up, and there are a lot of gaps in the commit message. Taken together that gives the signal that you don't really understand what you are doing.

That doesn't mean that you cannot fix that with another iteration though. But I'd suggest to take your time prepping the next iteration to read through the code, understand the concepts and doubt what AI spits out.

Thanks!

Patrick

#11 lore raw

v3 cover refs: run copy and rename through transactions

In reply to Maciej Ciemborowicz

Reference copy and rename bypass the transaction API. With files, the reference-transaction hook sees only the source deletion; with reftable, it sees neither endpoint. This series puts the logical ref updates and the reflog history into ordinary transactions so hooks can observe and reject the complete operation.

Thanks to Patrick, Junio and Karthik for their feedback. Following the review of v2, this version uses ref_transaction_update_reflog(), the API used by backend migration, to replay history. A new ref_transaction_replace_reflog() operation supplies the missing ability to discard destination history before installing the queued entries.

Changes since v2:

* Split the work into four patches: internal hook suppression, reflog replacement, copy/rename integration, and removal of the old callbacks.
* Remove the special copy/rename dispatch from backend prepare, finish and abort. Each destination points to its source update, so multiple copies, renames and ordinary updates can share one transaction.
* Keep the hook-suppression flag private and check it centrally in run_transaction_hook(). It is used for the physical packed-refs child transaction, whose changes the parent already reports.
* Drop the pre-lock snapshot revalidation. A preparing hook may change the source; prepared and committed report the value read under lock. Copy sources are locked but are not reported as changes to the hook.
* Stage reflog replacements during prepare. A prepared veto discards staging files without restoring old values over live refs or changing the source, destination or HEAD history.
* Add coverage for complete reflog contents, mixed transactions, hook vetoes, source races and failure paths, and measure the performance cost of replaying history.

Backend-specific work remains necessary to implement the transaction primitives. Files stages logs beside logs/refs using unique temporary files. For directory/file conflicts and case-only renames, it queues the destination in packed-refs: the loose source cannot remain visible while also making room for a loose destination lock. During finish, a backup preserves the source log until the destination log is installed; an installation error restores that backup. Reftable writes tombstones for old destination entries in the same table as the replacement.

The backends' final reflog records remain distinct: files appends old->old; reftable appends old->zero and zero->old for rename, or zero->old for copy. Forced reftable copies do change behavior: unrelated destination history is replaced by source history, matching files when the source has a reflog.

Replaying history requires O(N) time and memory. Buffered staging avoids a write system call per entry, but files rename loses its constant-time reflog move. On macOS/arm64, with no hook configured, median milliseconds per operation over seven samples of ten operations were:

                    entries       base        v3
  files copy             10       5.05      5.08
  files rename           10       4.88      5.26
  files copy          10000      13.20     16.11
  files rename        10000       5.09     17.09
  reftable copy          10       5.25      6.11
  reftable rename        10       5.81      6.50
  reftable copy       10000      53.39     68.06
  reftable rename     10000      43.44     54.06

Process startup is included. Copy overwrites the same destination; rename alternates between two names. Reftable starts from a migration of the same files fixture. Repeated operations add normal log entries and incur normal reftable compaction. Patch 3 adds t/perf/p1424-ref-copy-rename.sh; these measurements used a separate monotonic-clock driver because GNU time is not installed here. The files rename regression is a cost of this design, not just hook overhead.

Validation covered 15 relevant suites with files and reftable, including branch, update-ref, hooks, migration, reflogs and worktree refs. The two new suites contain 34 tests, with backend-specific skips; they also passed with SHA-256 and AddressSanitizer/UndefinedBehaviorSanitizer. The reflog prerequisite and main change were tested as intermediate trees. This was not a full test-suite run or a Linux/Windows run.

Files transactions are still not crash-atomic. A later failure in finish may leave partial ref changes, as with ordinary transactions. The source-log backup covers an installation error, not a process crash. Unrelated nested renames can also contend for the global packed-refs lock.

The base is 0f8e75abeb. This series is independent of the separate batched-deletion old-OID fix discussed elsewhere in the thread.

Original patch:
https://lore.kernel.org/git/[email protected]/

The range-diff below treats the rewritten and split implementation as four new commits, so the changes above describe the mapping from v2.

Maciej Ciemborowicz (4):
refs: distinguish internal transactions from logical updates
refs: support replacing reflogs in a transaction
refs: run copy and rename through ordinary transactions
refs: remove backend-specific copy and rename callbacks

 Documentation/githooks.adoc     |  10 +
 refs.c                          | 200 ++++++++-
 refs.h                          |  25 ++
 refs/debug.c                    |  24 --
 refs/files-backend.c            | 739 +++++++++++++++++---------------
 refs/packed-backend.c           |   2 -
 refs/refs-internal.h            |  28 +-
 refs/reftable-backend.c         | 334 ++-------------
 t/helper/test-ref-store.c       |  76 ++++
 t/perf/p1424-ref-copy-rename.sh |  48 +++
 t/t1424-ref-copy-transaction.sh | 352 +++++++++++++++
 t/t1425-reflog-transaction.sh   |  59 +++
 12 files changed, 1206 insertions(+), 691 deletions(-)
 create mode 100755 t/perf/p1424-ref-copy-rename.sh
 create mode 100755 t/t1424-ref-copy-transaction.sh
 create mode 100755 t/t1425-reflog-transaction.sh

Range-diff against v2:

1:  d852537d8c < -:  ---------- refs: run copy and rename through transactions
-:  ---------- > 1:  6d7c146e57 refs: distinguish internal transactions from logical updates
-:  ---------- > 2:  5c3ec4eb49 refs: support replacing reflogs in a transaction
-:  ---------- > 3:  77af4e809c refs: run copy and rename through ordinary transactions
-:  ---------- > 4:  83fa644fb3 refs: remove backend-specific copy and rename callbacks

base-commit: 0f8e75abebff0877cae681a3d5ff31ac47f54220

Signature · 1 line
2.39.3 (Apple Git-146)
#12 lore raw

PATCH v3 1/4 refs: distinguish internal transactions from logical updates

A ref backend may use a nested transaction to persist part of a logical update. Provide an internal-only transaction flag so that these physical updates can avoid reporting the same operation to reference-transaction hooks again.

Keep the check in run_transaction_hook(), covering every hook state in one place. The flag is deliberately not part of the public refs API. Subsequent changes use it for packed-refs updates belonging to a copy or rename.

---
 refs.c               | 3 +++
 refs/refs-internal.h | 3 +++
 2 files changed, 6 insertions(+)
refs.c +3 -0
index 92d5df5b71..00b1b51280 100644
@@ -2666,6 +2666,9 @@ static int run_transaction_hook(struct ref_transaction *transaction,
 	struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
 	int ret = 0;
 
+	if (transaction->flags & REF_TRANSACTION_FLAG_INTERNAL)
+		return 0;
+
 	strvec_push(&opt.args, state);
 
 	opt.feed_pipe = transaction_hook_feed_stdin;
refs/refs-internal.h +3 -0
index c3ac7b556f..0b41f5fa4b 100644
@@ -8,6 +8,9 @@
 struct fsck_options;
 struct ref_transaction;
 
+/* Physical updates nested in a transaction already reported to hooks. */
+#define REF_TRANSACTION_FLAG_INTERNAL (1 << 2)
+
 /*
  * Data structures and functions for the internal use of the refs
  * module. Code outside of the refs module should use only the public
Signature · 1 line
2.39.3 (Apple Git-146)
#13 lore raw

PATCH v3 2/4 refs: support replacing reflogs in a transaction

In reply to Maciej Ciemborowicz

Explicit log-only updates can already recreate a reflog during migration. Add ref_transaction_replace_reflog() to replace existing history with these entries without changing the reference itself. Keep its marker flag private to the refs implementation so callers cannot attach this cross-update behavior to an ordinary ref update accidentally.

Stage files-backend replacements in unique temporary files beside the logs and install them only during finish. Buffer writes and order entries by their requested index. Aborting prepare only removes staging files. In reftable, tombstone the previous entries in the same table as the replacement.

Do not treat a log-only entry with a null new OID as a reference
deletion: it is part of the history being replayed. Propagate read errors instead of silently copying a partial log.

Test preservation of null-OID entries, ordering, removal, hook rejection and duplicate replacement. This is preparation for copying and renaming through the ordinary transaction path.

---
 refs.c                        |  34 ++++++-
 refs.h                        |   9 ++
 refs/files-backend.c          | 180 +++++++++++++++++++++++++++++++---
 refs/refs-internal.h          |   3 +
 refs/reftable-backend.c       |  10 +-
 t/helper/test-ref-store.c     |  40 ++++++++
 t/t1425-reflog-transaction.sh |  59 +++++++++++
 7 files changed, 320 insertions(+), 15 deletions(-)
 create mode 100755 t/t1425-reflog-transaction.sh
refs.c +33 -1
index 00b1b51280..bf219573cd 100644
@@ -1413,7 +1413,6 @@ enum ref_transaction_error ref_transaction_update(struct ref_transaction *transa
 		strbuf_addstr(err, _("refusing to force and skip creation of reflog"));
 		return REF_TRANSACTION_ERROR_GENERIC;
 	}
-
 	if (!transaction_refname_valid(refname, new_oid, flags, err))
 		return REF_TRANSACTION_ERROR_GENERIC;
 
@@ -1496,6 +1495,39 @@ int ref_transaction_update_reflog(struct ref_transaction *transaction,
 	return 0;
 }
 
+int ref_transaction_replace_reflog(struct ref_transaction *transaction,
+				    const char *refname,
+				    struct strbuf *err)
+{
+	size_t i;
+	unsigned int flags = REF_LOG_ONLY | REF_NO_DEREF |
+		REF_SKIP_CREATE_REFLOG | REF_REPLACE_REFLOG;
+
+	assert(err);
+	if (transaction->flags &
+	    (REF_TRANSACTION_FLAG_INITIAL | REF_TRANSACTION_ALLOW_FAILURE)) {
+		strbuf_addstr(err, _("reflog replacement requires an "
+				     "all-or-nothing, non-initial transaction"));
+		return REF_TRANSACTION_ERROR_GENERIC;
+	}
+	if (!transaction_refname_valid(refname, NULL, flags, err))
+		return REF_TRANSACTION_ERROR_GENERIC;
+	for (i = 0; i < transaction->nr; i++) {
+		struct ref_update *update = transaction->updates[i];
+
+		if ((update->flags & REF_REPLACE_REFLOG) &&
+		    !strcmp(update->refname, refname)) {
+			strbuf_addf(err, _("reflog replacement for '%s' already queued"),
+				    refname);
+			return REF_TRANSACTION_ERROR_NAME_CONFLICT;
+		}
+	}
+
+	ref_transaction_add_update(transaction, refname, flags,
+				   NULL, NULL, NULL, NULL, NULL, NULL, NULL);
+	return 0;
+}
+
 int ref_transaction_create(struct ref_transaction *transaction,
 			   const char *refname,
 			   const struct object_id *new_oid,
refs.h +9 -0
index 9979446d15..7e80d0bdfb 100644
@@ -939,6 +939,15 @@ int ref_transaction_update_reflog(struct ref_transaction *transaction,
 				  uint64_t index,
 				  struct strbuf *err);
 
+/*
+ * Replace a reference's reflog with the explicit entries added to the same
+ * transaction via ref_transaction_update_reflog(). Without such entries,
+ * remove the reflog. This operation does not change the reference itself.
+ */
+int ref_transaction_replace_reflog(struct ref_transaction *transaction,
+				    const char *refname,
+				    struct strbuf *err);
+
 /*
  * Add a reference creation to transaction. new_oid is the value that
  * the reference should have after the update; it must not be
refs/files-backend.c +169 -11

240 lines not on this page · raw message

refs/refs-internal.h +3 -0
index 0b41f5fa4b..19fdf39d1d 100644
@@ -49,6 +49,9 @@ struct ref_transaction;
  */
 #define REF_HAVE_PEELED (1 << 15)
 
+/* Replace a reflog with the explicit log-only updates in the transaction. */
+#define REF_REPLACE_REFLOG (1 << 16)
+
 /*
  * Return the length of time to retry acquiring a loose reference lock
  * before giving up, in milliseconds:
refs/reftable-backend.c +7 -3
index 10db03991e..203b111f3d 100644
@@ -1503,9 +1503,11 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
 		 * - `core.logAllRefUpdates` tells us to create the reflog for
 		 *   the given ref.
 		 */
-		if ((u->flags & REF_HAVE_NEW) &&
+		if ((u->flags & REF_REPLACE_REFLOG) ||
+		    ((u->flags & REF_HAVE_NEW) &&
+		    !(u->flags & REF_LOG_ONLY) &&
 		    !(u->type & REF_ISSYMREF) &&
-		    ref_update_has_null_new_value(u)) {
+		    ref_update_has_null_new_value(u))) {
 			struct reftable_log_record log = {0};
 			struct reftable_iterator it = {0};
 
@@ -1548,8 +1550,10 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
 
 			if (ret)
 				goto done;
-		} else if (!(u->flags & REF_SKIP_CREATE_REFLOG) &&
+		}
+		if (!(u->flags & REF_SKIP_CREATE_REFLOG) &&
 			   (u->flags & REF_HAVE_NEW) &&
+			   (!ref_update_has_null_new_value(u) || (u->flags & REF_LOG_ONLY)) &&
 			   (u->flags & REF_FORCE_CREATE_REFLOG ||
 			    should_write_log(arg->refs, u->refname))) {
 			struct reftable_log_record *log;
t/helper/test-ref-store.c +40 -0
index db58f00589..7ce10c28af 100644
@@ -146,6 +146,45 @@ static int cmd_rename_ref(struct ref_store *refs, const char **argv)
 	return refs_rename_ref(refs, oldref, newref, logmsg);
 }
 
+static int cmd_reflog_transaction(struct ref_store *refs, const char **argv)
+{
+	const char *refname = notnull(*argv++, "refname");
+	const char *mode = notnull(*argv++, "mode");
+	struct strbuf err = STRBUF_INIT;
+	struct ref_transaction *transaction = ref_store_transaction_begin(refs, 0, &err);
+	int ret = !transaction;
+
+	if (!ret) {
+		if (!strcmp(mode, "replace")) {
+			ret = ref_transaction_replace_reflog(transaction, refname, &err);
+		} else if (!strcmp(mode, "replace-twice")) {
+			ret = ref_transaction_replace_reflog(transaction, refname, &err) ||
+				ref_transaction_replace_reflog(transaction, refname, &err);
+		} else if (strcmp(mode, "append")) {
+			die("unknown reflog transaction mode: %s", mode);
+		}
+	}
+	while (!ret && *argv) {
+		uint64_t index = strtoumax(notnull(*argv++, "index"), NULL, 10);
+		struct object_id old_oid, new_oid;
+		const char *message;
+
+		if (get_oid_hex(notnull(*argv++, "old-oid"), &old_oid) ||
+		    get_oid_hex(notnull(*argv++, "new-oid"), &new_oid))
+			die("invalid object ID");
+		message = notnull(*argv++, "message");
+		ret = ref_transaction_update_reflog(transaction, refname,
+				&new_oid, &old_oid, NULL, message, index, &err);
+	}
+	if (!ret)
+		ret = ref_transaction_commit(transaction, &err);
+	if (ret)
+		error("%s", err.buf);
+	ref_transaction_free(transaction);
+	strbuf_release(&err);
+	return ret;
+}
+
 static int each_ref(const struct reference *ref, void *cb_data UNUSED)
 {
 	printf("%s %s 0x%x\n", oid_to_hex(ref->oid), ref->name, ref->flags);
@@ -308,6 +347,7 @@ static struct command commands[] = {
 	{ "create-symref", cmd_create_symref },
 	{ "delete-refs", cmd_delete_refs },
 	{ "rename-ref", cmd_rename_ref },
+	{ "reflog-transaction", cmd_reflog_transaction },
 	{ "for-each-ref", cmd_for_each_ref },
 	{ "for-each-ref--exclude", cmd_for_each_ref__exclude },
 	{ "resolve-ref", cmd_resolve_ref },
t/t1425-reflog-transaction.sh new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
2.39.3 (Apple Git-146)
#14 lore raw

PATCH v3 3/4 refs: run copy and rename through ordinary transactions

In reply to Maciej Ciemborowicz

Copy and rename currently bypass reference transactions. A hook sees the source deletion with files and sees neither endpoint with reftable. Queue the source and destination as ordinary updates instead, with the destination referring to its source update.

Read and lock the source during prepare, then replay its history through the explicit reflog API. Keep this state per update so multiple independent operations and ordinary updates can share a transaction. Do not reject source changes made before locking: preparing is advisory, while prepared sees the values read under lock and unchanged on-disk refs and reflogs.

For files D/F and case-only renames, the source prevents locking a loose destination. Queue the destination in packed-refs, protected by the source lock (and an ancestor lock when needed). Suppress hooks for this physical child transaction only. Keep source logs until finish and use the staged replacements so a veto needs no rollback writes. During finish, retain a unique backup of a D/F source log until its destination log is installed, and restore it if installation fails.

Preserve the backends' distinct final reflog records. Forced reftable copies now replace destination history instead of retaining unrelated destination entries. Add regression tests for hooks, full histories, HEAD, source races, mixed transactions, lock and installation errors, restoration and cleanup, and a reproducible performance test.

Replaying history costs O(N) time and memory. In a local no-hook benchmark with 10,000 entries, files rename takes about 17 ms versus 5 ms before, and reftable rename about 54 ms versus 44 ms. Short-history operations remain within roughly 1 ms. This trades the files constant-time rename for staging that does not expose partial changes to a prepared hook.

---
 Documentation/githooks.adoc     |  10 +
 refs.c                          | 163 +++++++++++++--
 refs.h                          |  16 ++
 refs/files-backend.c            | 225 +++++++++++++++++++-
 refs/refs-internal.h            |  13 ++
 refs/reftable-backend.c         |  37 +++-
 t/helper/test-ref-store.c       |  36 ++++
 t/perf/p1424-ref-copy-rename.sh |  48 +++++
 t/t1424-ref-copy-transaction.sh | 352 ++++++++++++++++++++++++++++++++
 9 files changed, 880 insertions(+), 20 deletions(-)
 create mode 100755 t/perf/p1424-ref-copy-rename.sh
 create mode 100755 t/t1424-ref-copy-transaction.sh
Documentation/githooks.adoc +10 -0
index ed045940d1..629dd3e4d1 100644
@@ -518,6 +518,16 @@ distinguish these cases, you can inspect the current value of
 references are not resolved: `<ref-name>` will reflect the symbolic reference
 itself rather than the object it points to.
 
+Copying or renaming a reference is reported as one transaction. A rename
+reports the deletion of the source and the update of the destination;
+a copy reports only the destination update. Copying the reflog does not
+produce additional hook input. In the "preparing" state, the old values
+are unspecified (all zeroes), and the destination's new value is the
+source value read before locking. In subsequent states, these operations
+report the old values and the source value read under lock. Thus, a
+"preparing" hook may change the source before it is copied or renamed.
+A "prepared" hook still sees the original references and reflogs on disk.
+
 For symbolic reference updates the `<old_value>` and `<new-value>`
 fields could denote references instead of objects. A reference will be
 denoted with a 'ref:' prefix, like `ref:<ref-target>`.
refs.c +150 -13

178 lines not on this page · raw message

refs.h +16 -0
index 7e80d0bdfb..2538c611cd 100644
@@ -948,6 +948,22 @@ int ref_transaction_replace_reflog(struct ref_transaction *transaction,
 				    const char *refname,
 				    struct strbuf *err);
 
+/*
+ * Queue a copy or rename of a direct reference and its reflog. The source is
+ * read again under lock during prepare; changes made by the preparing hook
+ * are included. An existing destination is overwritten without dereferencing
+ * it. Several independent copies, renames and ordinary updates may be queued
+ * together. Each destination (and each source being renamed) must be unique.
+ * Initial transactions and transactions allowing individual failures are not
+ * supported. On failure, discard the transaction.
+ */
+int ref_transaction_copy(struct ref_transaction *transaction,
+			 const char *oldref, const char *newref,
+			 const char *logmsg, struct strbuf *err);
+int ref_transaction_rename(struct ref_transaction *transaction,
+			   const char *oldref, const char *newref,
+			   const char *logmsg, struct strbuf *err);
+
 /*
  * Add a reference creation to transaction. new_oid is the value that
  * the reference should have after the update; it must not be
refs/files-backend.c +220 -5

326 lines not on this page · raw message

refs/refs-internal.h +13 -0
index 19fdf39d1d..2746ce5b49 100644
@@ -52,6 +52,9 @@ struct ref_transaction;
 /* Replace a reflog with the explicit log-only updates in the transaction. */
 #define REF_REPLACE_REFLOG (1 << 16)
 
+/* A source whose value and reflog are consumed by a later update. */
+#define REF_COPY_SOURCE (1 << 17)
+
 /*
  * Return the length of time to retry acquiring a loose reference lock
  * before giving up, in milliseconds:
@@ -161,6 +164,9 @@ struct ref_update {
 	 */
 	struct ref_update *parent_update;
 
+	/* The source is queued first, so backends lock it before the destination. */
+	struct ref_update *copy_from;
+
 	const char refname[FLEX_ARRAY];
 };
 
@@ -193,6 +199,13 @@ struct ref_update *ref_transaction_add_update(
 		const char *committer_info,
 		const char *msg);
 
+int ref_update_record_copy_source(struct ref_update *update,
+				  const struct object_id *oid,
+				  struct strbuf *err);
+int ref_transaction_prepare_copy(struct ref_transaction *transaction,
+				 struct ref_update *update,
+				 struct strbuf *err);
+
 /*
  * Transaction states.
  *
refs/reftable-backend.c +35 -2
index 203b111f3d..72c1596c11 100644
@@ -1076,6 +1076,22 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
 	struct object_id current_oid = {0};
 	const char *rewritten_ref;
 
+	if (u->copy_from) {
+		const struct object_id *zero = null_oid(refs->base.repo->hash_algo);
+
+		if (ref_transaction_prepare_copy(transaction, u, err))
+			return REF_TRANSACTION_ERROR_GENERIC;
+		if ((u->copy_from->flags & REF_HAVE_NEW) &&
+		    ref_transaction_update_reflog(transaction, u->refname,
+				zero, &u->new_oid, NULL, u->msg,
+				transaction->max_index + 1, err))
+			return REF_TRANSACTION_ERROR_GENERIC;
+		if (ref_transaction_update_reflog(transaction, u->refname,
+				&u->new_oid, zero, NULL, u->msg,
+				transaction->max_index + 1, err))
+			return REF_TRANSACTION_ERROR_GENERIC;
+	}
+
 	/*
 	 * There is no need to reload the respective backends here as
 	 * we have already reloaded them when preparing the transaction
@@ -1126,15 +1142,27 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
 
 		ref_transaction_add_update(
 			transaction, "HEAD",
-			u->flags | REF_LOG_ONLY | REF_NO_DEREF,
+			(u->flags & ~REF_COPY_SOURCE) |
+			REF_LOG_ONLY | REF_NO_DEREF,
 			&u->new_oid, &u->old_oid, &u->peeled, NULL, NULL,
 			NULL, u->msg);
 	}
+	if (u->copy_from)
+		u->flags |= REF_SKIP_CREATE_REFLOG;
 
 	ret = reftable_backend_read_ref(be, rewritten_ref,
 					&current_oid, referent, &u->type);
 	if (ret < 0)
 		return REF_TRANSACTION_ERROR_GENERIC;
+	if (u->copy_from) {
+		oidcpy(&u->old_oid, &current_oid);
+		if (u->type & REF_ISSYMREF)
+			u->old_target = xstrdup(referent->buf);
+		u->flags |= REF_HAVE_OLD;
+	}
+	if ((u->flags & REF_COPY_SOURCE) &&
+	    ref_update_record_copy_source(u, &current_oid, err))
+		return REF_TRANSACTION_ERROR_GENERIC;
 	if (ret > 0 && !ref_update_expects_existing_old_ref(u)) {
 		struct string_list_item *item;
 		/*
@@ -1319,6 +1347,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 		reftable_be_downcast(ref_store, REF_STORE_WRITE|REF_STORE_MAIN, "ref_transaction_prepare");
 	struct strbuf referent = STRBUF_INIT, head_referent = STRBUF_INIT;
 	struct string_list refnames_to_check = STRING_LIST_INIT_NODUP;
+	struct string_list renamed_sources = STRING_LIST_INIT_NODUP;
 	struct reftable_transaction_data *tx_data = NULL;
 	struct reftable_backend *be;
 	struct object_id head_oid;
@@ -1338,6 +1367,9 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 	 * that will be modified during the transaction.
 	 */
 	for (i = 0; i < transaction->nr; i++) {
+		struct ref_update *update = transaction->updates[i];
+		if (update->copy_from && (update->copy_from->flags & REF_HAVE_NEW))
+			string_list_insert(&renamed_sources, update->copy_from->refname);
 		ret = prepare_transaction_update(NULL, refs, tx_data,
 						 transaction->updates[i], err);
 		if (ret)
@@ -1390,7 +1422,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 	}
 
 	ret = refs_verify_refnames_available(ref_store, &refnames_to_check,
-					     &transaction->refnames, NULL,
+					     &transaction->refnames, &renamed_sources,
 					     transaction,
 					     transaction->flags & REF_TRANSACTION_FLAG_INITIAL,
 					     err);
@@ -1411,6 +1443,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 	strbuf_release(&referent);
 	strbuf_release(&head_referent);
 	string_list_clear(&refnames_to_check, 1);
+	string_list_clear(&renamed_sources, 0);
 
 	return ret;
 }
t/helper/test-ref-store.c +36 -0
index 7ce10c28af..ec7728334c 100644
@@ -185,6 +185,41 @@ static int cmd_reflog_transaction(struct ref_store *refs, const char **argv)
 	return ret;
 }
 
+static int cmd_copy_transaction(struct ref_store *refs, const char **argv)
+{
+	struct strbuf err = STRBUF_INIT;
+	struct ref_transaction *transaction = ref_store_transaction_begin(refs, 0, &err);
+	int ret = !transaction;
+
+	while (!ret && *argv) {
+		const char *operation = *argv++;
+		const char *source = notnull(*argv++, "source");
+		const char *destination = notnull(*argv++, "destination");
+
+		if (!strcmp(operation, "rename"))
+			ret = ref_transaction_rename(transaction, source, destination, "rename", &err);
+		else if (!strcmp(operation, "copy"))
+			ret = ref_transaction_copy(transaction, source, destination, "copy", &err);
+		else if (!strcmp(operation, "update")) {
+			struct object_id oid;
+
+			if (get_oid_hex(destination, &oid))
+				die("invalid object ID: %s", destination);
+			ret = ref_transaction_update(transaction, source, &oid,
+						     NULL, NULL, NULL, 0, "update", &err);
+		} else {
+			die("unknown operation: %s", operation);
+		}
+	}
+	if (!ret)
+		ret = ref_transaction_commit(transaction, &err);
+	if (ret)
+		error("%s", err.buf);
+	ref_transaction_free(transaction);
+	strbuf_release(&err);
+	return ret;
+}
+
 static int each_ref(const struct reference *ref, void *cb_data UNUSED)
 {
 	printf("%s %s 0x%x\n", oid_to_hex(ref->oid), ref->name, ref->flags);
@@ -348,6 +383,7 @@ static struct command commands[] = {
 	{ "delete-refs", cmd_delete_refs },
 	{ "rename-ref", cmd_rename_ref },
 	{ "reflog-transaction", cmd_reflog_transaction },
+	{ "copy-transaction", cmd_copy_transaction },
 	{ "for-each-ref", cmd_for_each_ref },
 	{ "for-each-ref--exclude", cmd_for_each_ref__exclude },
 	{ "resolve-ref", cmd_resolve_ref },
t/perf/p1424-ref-copy-rename.sh new file +48 -0

48 lines not on this page · raw message

t/t1424-ref-copy-transaction.sh new file +352 -0

352 lines not on this page · raw message

Signature · 1 line
2.39.3 (Apple Git-146)
#15 lore raw

PATCH v3 4/4 refs: remove backend-specific copy and rename callbacks

In reply to Maciej Ciemborowicz

Copy and rename now use the transaction API for refs and reflogs. Remove the unused callbacks, backend implementations, and their private helpers. The files backend no longer needs a shared temporary reflog name or rollback writes that reconstruct previously visible refs and logs.

---
 refs/debug.c            |  24 ---
 refs/files-backend.c    | 334 ----------------------------------------
 refs/packed-backend.c   |   2 -
 refs/refs-internal.h    |   9 --
 refs/reftable-backend.c | 287 ----------------------------------
 5 files changed, 656 deletions(-)
refs/debug.c +0 -24
index 639db0f26e..b4991f71ae 100644
@@ -143,28 +143,6 @@ static int debug_optimize_required(struct ref_store *ref_store,
 	return res;
 }
 
-static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,
-			    const char *newref, const char *logmsg)
-{
-	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref,
-					      logmsg);
-	trace_printf_key(&trace_refs, "rename_ref: %s -> %s \"%s\": %d\n", oldref, newref,
-		logmsg, res);
-	return res;
-}
-
-static int debug_copy_ref(struct ref_store *ref_store, const char *oldref,
-			  const char *newref, const char *logmsg)
-{
-	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res =
-		drefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg);
-	trace_printf_key(&trace_refs, "copy_ref: %s -> %s \"%s\": %d\n", oldref, newref,
-		logmsg, res);
-	return res;
-}
-
 struct debug_ref_iterator {
 	struct ref_iterator base;
 	struct ref_iterator *iter;
@@ -453,8 +431,6 @@ struct ref_storage_be refs_be_debug = {
 	.optimize = debug_optimize,
 	.optimize_required = debug_optimize_required,
 
-	.rename_ref = debug_rename_ref,
-	.copy_ref = debug_copy_ref,
 
 	.iterator_begin = debug_ref_iterator_begin,
 	.read_raw_ref = debug_read_raw_ref,
refs/files-backend.c +0 -334

352 lines not on this page · raw message

refs/packed-backend.c +0 -2
index a73fc6aca7..364a912912 100644
@@ -2164,8 +2164,6 @@ struct ref_storage_be refs_be_packed = {
 	.optimize = packed_optimize,
 	.optimize_required = packed_optimize_required,
 
-	.rename_ref = NULL,
-	.copy_ref = NULL,
 
 	.iterator_begin = packed_ref_iterator_begin,
 	.read_raw_ref = packed_read_raw_ref,
refs/refs-internal.h +0 -9
index 2746ce5b49..c415ba0a4e 100644
@@ -470,13 +470,6 @@ typedef int optimize_required_fn(struct ref_store *ref_store,
 				 struct refs_optimize_opts *opts,
 				 bool *required);
 
-typedef int rename_ref_fn(struct ref_store *ref_store,
-			  const char *oldref, const char *newref,
-			  const char *logmsg);
-typedef int copy_ref_fn(struct ref_store *ref_store,
-			  const char *oldref, const char *newref,
-			  const char *logmsg);
-
 /*
  * Iterate over the references in `ref_store` whose names start with
  * `prefix`. `prefix` is matched as a literal string, without regard
@@ -596,8 +589,6 @@ struct ref_storage_be {
 
 	optimize_fn *optimize;
 	optimize_required_fn *optimize_required;
-	rename_ref_fn *rename_ref;
-	copy_ref_fn *copy_ref;
 
 	ref_iterator_begin_fn *iterator_begin;
 	read_raw_ref_fn *read_raw_ref;
refs/reftable-backend.c +0 -287

299 lines not on this page · raw message

Signature · 1 line
2.39.3 (Apple Git-146)
#16 lore raw

Re: [PATCH v3 0/4] refs: run copy and rename through transactions

In reply to Maciej Ciemborowicz
Quoted 23 lines · Maciej Ciemborowicz

Reference copy and rename bypass the transaction API. With files, the reference-transaction hook sees only the source deletion; with reftable, it sees neither endpoint. This series puts the logical ref updates and the reflog history into ordinary transactions so hooks can observe and reject the complete operation.
...

 Documentation/githooks.adoc     |  10 +
 refs.c                          | 200 ++++++++-
 refs.h                          |  25 ++
 refs/debug.c                    |  24 --
 refs/files-backend.c            | 739 +++++++++++++++++---------------
 refs/packed-backend.c           |   2 -
 refs/refs-internal.h            |  28 +-
 refs/reftable-backend.c         | 334 ++-------------
 t/helper/test-ref-store.c       |  76 ++++
 t/perf/p1424-ref-copy-rename.sh |  48 +++
 t/t1424-ref-copy-transaction.sh | 352 +++++++++++++++
 t/t1425-reflog-transaction.sh   |  59 +++
 12 files changed, 1206 insertions(+), 691 deletions(-)
 create mode 100755 t/perf/p1424-ref-copy-rename.sh
 create mode 100755 t/t1424-ref-copy-transaction.sh
 create mode 100755 t/t1425-reflog-transaction.sh
Quoted 6 lines

Range-diff against v2:

1:  d852537d8c < -:  ---------- refs: run copy and rename through transactions
-:  ---------- > 1:  6d7c146e57 refs: distinguish internal transactions from logical updates
-:  ---------- > 2:  5c3ec4eb49 refs: support replacing reflogs in a transaction
-:  ---------- > 3:  77af4e809c refs: run copy and rename through ordinary transactions
-:  ---------- > 4:  83fa644fb3 refs: remove backend-specific copy and rename callbacks

I do not think I have time to read these humoungous patches, some of which weigh more than 1000+ lines, with fine toothed comb any time soon, but when applied to the recent tip of master 6de20f6092 (The 4th batch, 2026-10-06), it seems to break t0600 and t5510. The failing tests do not seem to be anything so system specific (and I am on a GNU/Linux platform that is not anything remarkable).

I wonder what I am doing differently. Did they pass for you? Here are the first failure from these test scripts.

Thanks.

expecting success of 0600.16 'delete fails cleanly if packed-refs.new write fails':

        # Setup and expectations are similar to the test above.
        prefix=refs/failed-packed-refs &&
        git update-ref $prefix/foo $C &&
        git pack-refs --all &&
        git update-ref $prefix/foo $D &&
        git for-each-ref $prefix >unchanged &&
        # This should not happen in practice, but it is an easy way to get a
        # reliable error (we open with create_tempfile(), which uses O_EXCL).
        : >.git/packed-refs.new &&
        test_when_finished "rm -f .git/packed-refs.new" &&
        test_must_fail git update-ref -d $prefix/foo &&
        git for-each-ref $prefix >actual &&
        test_cmp unchanged actual

test_must_fail: command succeeded: git update-ref -d refs/failed-packed-refs/foo not ok 16 - delete fails cleanly if packed-refs.new write fails

expecting success of 5510.35 'fetch --prune fails to delete branches':

        git clone . prune-fail &&
        (
                cd prune-fail &&
                git update-ref refs/remotes/origin/extrabranch main &&
                git pack-refs --all &&
                : this will prevent --prune from locking packed-refs for deleting refs, but adding loose refs still succeeds  &&
                >.git/packed-refs.new &&

                test_must_fail git fetch --prune origin
        )

Cloning into 'prune-fail'...
done.
From /usr/local/google/home/jch/w/git.git/t/trash directory.t5510-fetch/.
- [deleted] (none) -> origin/extrabranch
test_must_fail: command succeeded: git fetch --prune origin not ok 35 - fetch --prune fails to delete branches

#17 lore raw

v4 cover refs: run copy and rename through transactions

In reply to Maciej Ciemborowicz

Reference copy and rename bypass the transaction API. With files, the reference-transaction hook sees only the source deletion; with reftable, it sees neither endpoint. This series represents the logical ref updates and reflog history through ordinary transactions, allowing hooks to observe and reject the operation.

Junio, thank you for reporting the t0600 and t5510 failures in v3. I reproduced both on macOS with the original base as well. I had not run those suites before submitting v3; they were outside the test selection reported in that cover letter. That was a gap in my validation, and I am sorry for sending the regression.

Patch 2 added a call to prepare_reflog_replacements() after preparing the packed transaction. If packed preparation failed, its error was saved in ret, but the new call overwrote it with success. The files transaction could then proceed despite the failure to prepare packed-refs. The fix jumps to cleanup after freeing the failed packed transaction, preserving the error and releasing the remaining resources. The existing t0600.16 and t5510.35 cover this path; their expectations have not been changed.

Changes since v3:

* Rebase onto 6de20f6092 (The 4th batch, 2026-10-06), the master commit used in Junio's report.
* Preserve the packed preparation error in patch 2 as described above.
* Register t1425 and t1424 in t/meson.build in the commits adding them.

The four-patch structure and implementation otherwise remain unchanged. This iteration does not address the concern about the size of the main patch; the range-diff below isolates the correction and registrations.

I ran the complete standard core test collection, including the unit-test executable, on macOS/arm64 with each default ref backend:

files: Files=1064, Tests=34710, Result: PASS
reftable: Files=1064, Tests=34712, Result: PASS

Both full runs include successful t0600, t5510, t1424 and t1425 runs. The build enables Perl, Python, cURL and gettext. The files run used:

  LC_ALL=C make -j8 PYTHON_PATH=/opt/homebrew/bin/python3 \
    GNU_GETTEXT_PATH=/opt/homebrew/opt/gettext \
    DEFAULT_TEST_TARGET=prove GIT_PROVE_OPTS='--jobs 8' test

The reftable run used GIT_TEST_DEFAULT_REF_FORMAT=reftable and ran every t[0-9][0-9][0-9][0-9]-*.sh plus unit-tests/bin/unit-tests through prove, with four jobs and a separate TEST_OUTPUT_DIRECTORY. Backend-specific suites can override the default. The Meson registration check and git diff --check also pass. The contrib test target completed with no unexpected failures; diff-highlight retains two known TODO failures.

The harness totals include skips and expected failures. The files run skipped 126 whole scripts, mostly for unavailable SVN, Perforce and CVS tools. Other skips include GPG, JGit, Windows-specific tests, tests requiring sudo or writable /, case-sensitive filesystem tests, the opt-in 2GB clone test, and t5564 because its web-server setup failed. There are also individual prerequisite-based skips within suites. I have not validated this iteration on Linux or Windows.

The performance trade-off described in v3 remains: replaying history uses O(N) time and memory, replacing the files backend's constant-time reflog rename. The measurements in patch 3 are from the v3 base, not a new benchmark on this base. Files transactions also remain non-atomic with respect to process crashes and failures late in finish.

Previous iteration:
https://lore.kernel.org/git/[email protected]/

Maciej Ciemborowicz (4):
refs: distinguish internal transactions from logical updates
refs: support replacing reflogs in a transaction
refs: run copy and rename through ordinary transactions
refs: remove backend-specific copy and rename callbacks

 Documentation/githooks.adoc     |  10 +
 refs.c                          | 200 ++++++++-
 refs.h                          |  25 ++
 refs/debug.c                    |  24 --
 refs/files-backend.c            | 740 +++++++++++++++++---------------
 refs/packed-backend.c           |   2 -
 refs/refs-internal.h            |  28 +-
 refs/reftable-backend.c         | 334 ++------------
 t/helper/test-ref-store.c       |  76 ++++
 t/meson.build                   |   2 +
 t/perf/p1424-ref-copy-rename.sh |  48 +++
 t/t1424-ref-copy-transaction.sh | 352 +++++++++++++++
 t/t1425-reflog-transaction.sh   |  59 +++
 13 files changed, 1209 insertions(+), 691 deletions(-)
 create mode 100755 t/perf/p1424-ref-copy-rename.sh
 create mode 100755 t/t1424-ref-copy-transaction.sh
 create mode 100755 t/t1425-reflog-transaction.sh

Range-diff against v3:

1:  6d7c146e57 = 1:  948927d8fb refs: distinguish internal transactions from logical updates
2:  5c3ec4eb49 ! 2:  d023da3c09 refs: support replacing reflogs in a transaction

@@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref
transaction->backend_data = backend_data;

/*
+@@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref_store,
+ if (ret) {
+ ref_transaction_free(packed_transaction);
+ backend_data->packed_transaction = NULL;
++ goto cleanup;
+ }
+ } else {
+ /*
@@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref_store,
}
}
@@ t/helper/test-ref-store.c: static struct command commands[] = {
{ "for-each-ref--exclude", cmd_for_each_ref__exclude },
{ "resolve-ref", cmd_resolve_ref },

+ ## t/meson.build ##
+@@ t/meson.build: integration_tests = [
+ 't1421-reflog-write.sh',
+ 't1422-show-ref-exists.sh',
+ 't1423-ref-backend.sh',
++ 't1425-reflog-transaction.sh',
+ 't1430-bad-ref-name.sh',
+ 't1450-fsck.sh',
+ 't1451-fsck-buffer.sh',
+
## t/t1425-reflog-transaction.sh (new) ##
@@
+#!/bin/sh
3: 77af4e809c ! 3: 150349f9d0 refs: run copy and rename through ordinary transactions
@@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref
- if (update->flags & REF_DELETING &&
+ if (update->flags & (REF_DELETING | REF_NEEDS_PACK) &&
!(update->flags & REF_LOG_ONLY) &&
- !(update->flags & REF_IS_PRUNING)) {
- /*
+ !(update->flags & REF_IS_PRUNING) &&
+ !is_root_ref(update->refname)) {
@@ refs/files-backend.c: static int files_transaction_prepare(struct ref_store *ref_store,
REF_HAVE_NEW | REF_NO_DEREF, &update->new_oid, NULL, NULL,
@@ t/helper/test-ref-store.c: static struct command commands[] = {
{ "for-each-ref--exclude", cmd_for_each_ref__exclude },
{ "resolve-ref", cmd_resolve_ref },

+ ## t/meson.build ##
+@@ t/meson.build: integration_tests = [
+ 't1421-reflog-write.sh',
+ 't1422-show-ref-exists.sh',
+ 't1423-ref-backend.sh',
++ 't1424-ref-copy-transaction.sh',
+ 't1425-reflog-transaction.sh',
+ 't1430-bad-ref-name.sh',
+ 't1450-fsck.sh',
+
## t/perf/p1424-ref-copy-rename.sh (new) ##
@@
+#!/bin/sh
4: 83fa644fb3 = 4: f3f2c7ee11 refs: remove backend-specific copy and rename callbacks

base-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd

Signature · 1 line
2.39.3 (Apple Git-146)
#18 lore raw

PATCH v4 1/4 refs: distinguish internal transactions from logical updates

A ref backend may use a nested transaction to persist part of a logical update. Provide an internal-only transaction flag so that these physical updates can avoid reporting the same operation to reference-transaction hooks again.

Keep the check in run_transaction_hook(), covering every hook state in one place. The flag is deliberately not part of the public refs API. Subsequent changes use it for packed-refs updates belonging to a copy or rename.

---
 refs.c               | 3 +++
 refs/refs-internal.h | 3 +++
 2 files changed, 6 insertions(+)
refs.c +3 -0
index 951db56113..642f895b71 100644
@@ -2689,6 +2689,9 @@ static int run_transaction_hook(struct ref_transaction *transaction,
 	struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
 	int ret = 0;
 
+	if (transaction->flags & REF_TRANSACTION_FLAG_INTERNAL)
+		return 0;
+
 	strvec_push(&opt.args, state);
 
 	opt.feed_pipe = transaction_hook_feed_stdin;
refs/refs-internal.h +3 -0
index c3ac7b556f..0b41f5fa4b 100644
@@ -8,6 +8,9 @@
 struct fsck_options;
 struct ref_transaction;
 
+/* Physical updates nested in a transaction already reported to hooks. */
+#define REF_TRANSACTION_FLAG_INTERNAL (1 << 2)
+
 /*
  * Data structures and functions for the internal use of the refs
  * module. Code outside of the refs module should use only the public
Signature · 1 line
2.39.3 (Apple Git-146)
#19 lore raw

PATCH v4 2/4 refs: support replacing reflogs in a transaction

In reply to Maciej Ciemborowicz

Explicit log-only updates can already recreate a reflog during migration. Add ref_transaction_replace_reflog() to replace existing history with these entries without changing the reference itself. Keep its marker flag private to the refs implementation so callers cannot attach this cross-update behavior to an ordinary ref update accidentally.

Stage files-backend replacements in unique temporary files beside the logs and install them only during finish. Buffer writes and order entries by their requested index. Aborting prepare only removes staging files. In reftable, tombstone the previous entries in the same table as the replacement.

Do not treat a log-only entry with a null new OID as a reference
deletion: it is part of the history being replayed. Propagate read errors instead of silently copying a partial log.

Test preservation of null-OID entries, ordering, removal, hook rejection and duplicate replacement. This is preparation for copying and renaming through the ordinary transaction path.

---
 refs.c                        |  34 ++++++-
 refs.h                        |   9 ++
 refs/files-backend.c          | 181 +++++++++++++++++++++++++++++++---
 refs/refs-internal.h          |   3 +
 refs/reftable-backend.c       |  10 +-
 t/helper/test-ref-store.c     |  40 ++++++++
 t/meson.build                 |   1 +
 t/t1425-reflog-transaction.sh |  59 +++++++++++
 8 files changed, 322 insertions(+), 15 deletions(-)
 create mode 100755 t/t1425-reflog-transaction.sh
refs.c +33 -1
index 642f895b71..c02da9d966 100644
@@ -1436,7 +1436,6 @@ enum ref_transaction_error ref_transaction_update(struct ref_transaction *transa
 		strbuf_addstr(err, _("refusing to force and skip creation of reflog"));
 		return REF_TRANSACTION_ERROR_GENERIC;
 	}
-
 	if (!transaction_refname_valid(refname, new_oid, flags, err))
 		return REF_TRANSACTION_ERROR_GENERIC;
 
@@ -1519,6 +1518,39 @@ int ref_transaction_update_reflog(struct ref_transaction *transaction,
 	return 0;
 }
 
+int ref_transaction_replace_reflog(struct ref_transaction *transaction,
+				    const char *refname,
+				    struct strbuf *err)
+{
+	size_t i;
+	unsigned int flags = REF_LOG_ONLY | REF_NO_DEREF |
+		REF_SKIP_CREATE_REFLOG | REF_REPLACE_REFLOG;
+
+	assert(err);
+	if (transaction->flags &
+	    (REF_TRANSACTION_FLAG_INITIAL | REF_TRANSACTION_ALLOW_FAILURE)) {
+		strbuf_addstr(err, _("reflog replacement requires an "
+				     "all-or-nothing, non-initial transaction"));
+		return REF_TRANSACTION_ERROR_GENERIC;
+	}
+	if (!transaction_refname_valid(refname, NULL, flags, err))
+		return REF_TRANSACTION_ERROR_GENERIC;
+	for (i = 0; i < transaction->nr; i++) {
+		struct ref_update *update = transaction->updates[i];
+
+		if ((update->flags & REF_REPLACE_REFLOG) &&
+		    !strcmp(update->refname, refname)) {
+			strbuf_addf(err, _("reflog replacement for '%s' already queued"),
+				    refname);
+			return REF_TRANSACTION_ERROR_NAME_CONFLICT;
+		}
+	}
+
+	ref_transaction_add_update(transaction, refname, flags,
+				   NULL, NULL, NULL, NULL, NULL, NULL, NULL);
+	return 0;
+}
+
 int ref_transaction_create(struct ref_transaction *transaction,
 			   const char *refname,
 			   const struct object_id *new_oid,
refs.h +9 -0
index ee3b8a62ef..b0b2b0e4fd 100644
@@ -943,6 +943,15 @@ int ref_transaction_update_reflog(struct ref_transaction *transaction,
 				  uint64_t index,
 				  struct strbuf *err);
 
+/*
+ * Replace a reference's reflog with the explicit entries added to the same
+ * transaction via ref_transaction_update_reflog(). Without such entries,
+ * remove the reflog. This operation does not change the reference itself.
+ */
+int ref_transaction_replace_reflog(struct ref_transaction *transaction,
+				    const char *refname,
+				    struct strbuf *err);
+
 /*
  * Add a reference creation to transaction. new_oid is the value that
  * the reference should have after the update; it must not be
refs/files-backend.c +170 -11

247 lines not on this page · raw message

refs/refs-internal.h +3 -0
index 0b41f5fa4b..19fdf39d1d 100644
@@ -49,6 +49,9 @@ struct ref_transaction;
  */
 #define REF_HAVE_PEELED (1 << 15)
 
+/* Replace a reflog with the explicit log-only updates in the transaction. */
+#define REF_REPLACE_REFLOG (1 << 16)
+
 /*
  * Return the length of time to retry acquiring a loose reference lock
  * before giving up, in milliseconds:
refs/reftable-backend.c +7 -3
index 10db03991e..203b111f3d 100644
@@ -1503,9 +1503,11 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
 		 * - `core.logAllRefUpdates` tells us to create the reflog for
 		 *   the given ref.
 		 */
-		if ((u->flags & REF_HAVE_NEW) &&
+		if ((u->flags & REF_REPLACE_REFLOG) ||
+		    ((u->flags & REF_HAVE_NEW) &&
+		    !(u->flags & REF_LOG_ONLY) &&
 		    !(u->type & REF_ISSYMREF) &&
-		    ref_update_has_null_new_value(u)) {
+		    ref_update_has_null_new_value(u))) {
 			struct reftable_log_record log = {0};
 			struct reftable_iterator it = {0};
 
@@ -1548,8 +1550,10 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
 
 			if (ret)
 				goto done;
-		} else if (!(u->flags & REF_SKIP_CREATE_REFLOG) &&
+		}
+		if (!(u->flags & REF_SKIP_CREATE_REFLOG) &&
 			   (u->flags & REF_HAVE_NEW) &&
+			   (!ref_update_has_null_new_value(u) || (u->flags & REF_LOG_ONLY)) &&
 			   (u->flags & REF_FORCE_CREATE_REFLOG ||
 			    should_write_log(arg->refs, u->refname))) {
 			struct reftable_log_record *log;
t/helper/test-ref-store.c +40 -0
index db58f00589..7ce10c28af 100644
@@ -146,6 +146,45 @@ static int cmd_rename_ref(struct ref_store *refs, const char **argv)
 	return refs_rename_ref(refs, oldref, newref, logmsg);
 }
 
+static int cmd_reflog_transaction(struct ref_store *refs, const char **argv)
+{
+	const char *refname = notnull(*argv++, "refname");
+	const char *mode = notnull(*argv++, "mode");
+	struct strbuf err = STRBUF_INIT;
+	struct ref_transaction *transaction = ref_store_transaction_begin(refs, 0, &err);
+	int ret = !transaction;
+
+	if (!ret) {
+		if (!strcmp(mode, "replace")) {
+			ret = ref_transaction_replace_reflog(transaction, refname, &err);
+		} else if (!strcmp(mode, "replace-twice")) {
+			ret = ref_transaction_replace_reflog(transaction, refname, &err) ||
+				ref_transaction_replace_reflog(transaction, refname, &err);
+		} else if (strcmp(mode, "append")) {
+			die("unknown reflog transaction mode: %s", mode);
+		}
+	}
+	while (!ret && *argv) {
+		uint64_t index = strtoumax(notnull(*argv++, "index"), NULL, 10);
+		struct object_id old_oid, new_oid;
+		const char *message;
+
+		if (get_oid_hex(notnull(*argv++, "old-oid"), &old_oid) ||
+		    get_oid_hex(notnull(*argv++, "new-oid"), &new_oid))
+			die("invalid object ID");
+		message = notnull(*argv++, "message");
+		ret = ref_transaction_update_reflog(transaction, refname,
+				&new_oid, &old_oid, NULL, message, index, &err);
+	}
+	if (!ret)
+		ret = ref_transaction_commit(transaction, &err);
+	if (ret)
+		error("%s", err.buf);
+	ref_transaction_free(transaction);
+	strbuf_release(&err);
+	return ret;
+}
+
 static int each_ref(const struct reference *ref, void *cb_data UNUSED)
 {
 	printf("%s %s 0x%x\n", oid_to_hex(ref->oid), ref->name, ref->flags);
@@ -308,6 +347,7 @@ static struct command commands[] = {
 	{ "create-symref", cmd_create_symref },
 	{ "delete-refs", cmd_delete_refs },
 	{ "rename-ref", cmd_rename_ref },
+	{ "reflog-transaction", cmd_reflog_transaction },
 	{ "for-each-ref", cmd_for_each_ref },
 	{ "for-each-ref--exclude", cmd_for_each_ref__exclude },
 	{ "resolve-ref", cmd_resolve_ref },
t/meson.build +1 -0
index f65eb04684..fb5266cd0d 100644
@@ -218,6 +218,7 @@ integration_tests = [
   't1421-reflog-write.sh',
   't1422-show-ref-exists.sh',
   't1423-ref-backend.sh',
+  't1425-reflog-transaction.sh',
   't1430-bad-ref-name.sh',
   't1450-fsck.sh',
   't1451-fsck-buffer.sh',
t/t1425-reflog-transaction.sh new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
2.39.3 (Apple Git-146)
#20 lore raw

PATCH v4 3/4 refs: run copy and rename through ordinary transactions

In reply to Maciej Ciemborowicz

Copy and rename currently bypass reference transactions. A hook sees the source deletion with files and sees neither endpoint with reftable. Queue the source and destination as ordinary updates instead, with the destination referring to its source update.

Read and lock the source during prepare, then replay its history through the explicit reflog API. Keep this state per update so multiple independent operations and ordinary updates can share a transaction. Do not reject source changes made before locking: preparing is advisory, while prepared sees the values read under lock and unchanged on-disk refs and reflogs.

For files D/F and case-only renames, the source prevents locking a loose destination. Queue the destination in packed-refs, protected by the source lock (and an ancestor lock when needed). Suppress hooks for this physical child transaction only. Keep source logs until finish and use the staged replacements so a veto needs no rollback writes. During finish, retain a unique backup of a D/F source log until its destination log is installed, and restore it if installation fails.

Preserve the backends' distinct final reflog records. Forced reftable copies now replace destination history instead of retaining unrelated destination entries. Add regression tests for hooks, full histories, HEAD, source races, mixed transactions, lock and installation errors, restoration and cleanup, and a reproducible performance test.

Replaying history costs O(N) time and memory. In a local no-hook benchmark with 10,000 entries, files rename takes about 17 ms versus 5 ms before, and reftable rename about 54 ms versus 44 ms. Short-history operations remain within roughly 1 ms. This trades the files constant-time rename for staging that does not expose partial changes to a prepared hook.

---
 Documentation/githooks.adoc     |  10 +
 refs.c                          | 163 +++++++++++++--
 refs.h                          |  16 ++
 refs/files-backend.c            | 225 +++++++++++++++++++-
 refs/refs-internal.h            |  13 ++
 refs/reftable-backend.c         |  37 +++-
 t/helper/test-ref-store.c       |  36 ++++
 t/meson.build                   |   1 +
 t/perf/p1424-ref-copy-rename.sh |  48 +++++
 t/t1424-ref-copy-transaction.sh | 352 ++++++++++++++++++++++++++++++++
 10 files changed, 881 insertions(+), 20 deletions(-)
 create mode 100755 t/perf/p1424-ref-copy-rename.sh
 create mode 100755 t/t1424-ref-copy-transaction.sh
Documentation/githooks.adoc +10 -0
index 145642bf05..245fec3b70 100644
@@ -518,6 +518,16 @@ distinguish these cases, you can inspect the current value of
 references are not resolved: `<ref-name>` will reflect the symbolic reference
 itself rather than the object it points to.
 
+Copying or renaming a reference is reported as one transaction. A rename
+reports the deletion of the source and the update of the destination;
+a copy reports only the destination update. Copying the reflog does not
+produce additional hook input. In the "preparing" state, the old values
+are unspecified (all zeroes), and the destination's new value is the
+source value read before locking. In subsequent states, these operations
+report the old values and the source value read under lock. Thus, a
+"preparing" hook may change the source before it is copied or renamed.
+A "prepared" hook still sees the original references and reflogs on disk.
+
 For symbolic reference updates the `<old_value>` and `<new-value>`
 fields could denote references instead of objects. A reference will be
 denoted with a 'ref:' prefix, like `ref:<ref-target>`.
refs.c +150 -13

178 lines not on this page · raw message

refs.h +16 -0
index b0b2b0e4fd..eba5025351 100644
@@ -952,6 +952,22 @@ int ref_transaction_replace_reflog(struct ref_transaction *transaction,
 				    const char *refname,
 				    struct strbuf *err);
 
+/*
+ * Queue a copy or rename of a direct reference and its reflog. The source is
+ * read again under lock during prepare; changes made by the preparing hook
+ * are included. An existing destination is overwritten without dereferencing
+ * it. Several independent copies, renames and ordinary updates may be queued
+ * together. Each destination (and each source being renamed) must be unique.
+ * Initial transactions and transactions allowing individual failures are not
+ * supported. On failure, discard the transaction.
+ */
+int ref_transaction_copy(struct ref_transaction *transaction,
+			 const char *oldref, const char *newref,
+			 const char *logmsg, struct strbuf *err);
+int ref_transaction_rename(struct ref_transaction *transaction,
+			   const char *oldref, const char *newref,
+			   const char *logmsg, struct strbuf *err);
+
 /*
  * Add a reference creation to transaction. new_oid is the value that
  * the reference should have after the update; it must not be
refs/files-backend.c +220 -5

326 lines not on this page · raw message

refs/refs-internal.h +13 -0
index 19fdf39d1d..2746ce5b49 100644
@@ -52,6 +52,9 @@ struct ref_transaction;
 /* Replace a reflog with the explicit log-only updates in the transaction. */
 #define REF_REPLACE_REFLOG (1 << 16)
 
+/* A source whose value and reflog are consumed by a later update. */
+#define REF_COPY_SOURCE (1 << 17)
+
 /*
  * Return the length of time to retry acquiring a loose reference lock
  * before giving up, in milliseconds:
@@ -161,6 +164,9 @@ struct ref_update {
 	 */
 	struct ref_update *parent_update;
 
+	/* The source is queued first, so backends lock it before the destination. */
+	struct ref_update *copy_from;
+
 	const char refname[FLEX_ARRAY];
 };
 
@@ -193,6 +199,13 @@ struct ref_update *ref_transaction_add_update(
 		const char *committer_info,
 		const char *msg);
 
+int ref_update_record_copy_source(struct ref_update *update,
+				  const struct object_id *oid,
+				  struct strbuf *err);
+int ref_transaction_prepare_copy(struct ref_transaction *transaction,
+				 struct ref_update *update,
+				 struct strbuf *err);
+
 /*
  * Transaction states.
  *
refs/reftable-backend.c +35 -2
index 203b111f3d..72c1596c11 100644
@@ -1076,6 +1076,22 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
 	struct object_id current_oid = {0};
 	const char *rewritten_ref;
 
+	if (u->copy_from) {
+		const struct object_id *zero = null_oid(refs->base.repo->hash_algo);
+
+		if (ref_transaction_prepare_copy(transaction, u, err))
+			return REF_TRANSACTION_ERROR_GENERIC;
+		if ((u->copy_from->flags & REF_HAVE_NEW) &&
+		    ref_transaction_update_reflog(transaction, u->refname,
+				zero, &u->new_oid, NULL, u->msg,
+				transaction->max_index + 1, err))
+			return REF_TRANSACTION_ERROR_GENERIC;
+		if (ref_transaction_update_reflog(transaction, u->refname,
+				&u->new_oid, zero, NULL, u->msg,
+				transaction->max_index + 1, err))
+			return REF_TRANSACTION_ERROR_GENERIC;
+	}
+
 	/*
 	 * There is no need to reload the respective backends here as
 	 * we have already reloaded them when preparing the transaction
@@ -1126,15 +1142,27 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
 
 		ref_transaction_add_update(
 			transaction, "HEAD",
-			u->flags | REF_LOG_ONLY | REF_NO_DEREF,
+			(u->flags & ~REF_COPY_SOURCE) |
+			REF_LOG_ONLY | REF_NO_DEREF,
 			&u->new_oid, &u->old_oid, &u->peeled, NULL, NULL,
 			NULL, u->msg);
 	}
+	if (u->copy_from)
+		u->flags |= REF_SKIP_CREATE_REFLOG;
 
 	ret = reftable_backend_read_ref(be, rewritten_ref,
 					&current_oid, referent, &u->type);
 	if (ret < 0)
 		return REF_TRANSACTION_ERROR_GENERIC;
+	if (u->copy_from) {
+		oidcpy(&u->old_oid, &current_oid);
+		if (u->type & REF_ISSYMREF)
+			u->old_target = xstrdup(referent->buf);
+		u->flags |= REF_HAVE_OLD;
+	}
+	if ((u->flags & REF_COPY_SOURCE) &&
+	    ref_update_record_copy_source(u, &current_oid, err))
+		return REF_TRANSACTION_ERROR_GENERIC;
 	if (ret > 0 && !ref_update_expects_existing_old_ref(u)) {
 		struct string_list_item *item;
 		/*
@@ -1319,6 +1347,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 		reftable_be_downcast(ref_store, REF_STORE_WRITE|REF_STORE_MAIN, "ref_transaction_prepare");
 	struct strbuf referent = STRBUF_INIT, head_referent = STRBUF_INIT;
 	struct string_list refnames_to_check = STRING_LIST_INIT_NODUP;
+	struct string_list renamed_sources = STRING_LIST_INIT_NODUP;
 	struct reftable_transaction_data *tx_data = NULL;
 	struct reftable_backend *be;
 	struct object_id head_oid;
@@ -1338,6 +1367,9 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 	 * that will be modified during the transaction.
 	 */
 	for (i = 0; i < transaction->nr; i++) {
+		struct ref_update *update = transaction->updates[i];
+		if (update->copy_from && (update->copy_from->flags & REF_HAVE_NEW))
+			string_list_insert(&renamed_sources, update->copy_from->refname);
 		ret = prepare_transaction_update(NULL, refs, tx_data,
 						 transaction->updates[i], err);
 		if (ret)
@@ -1390,7 +1422,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 	}
 
 	ret = refs_verify_refnames_available(ref_store, &refnames_to_check,
-					     &transaction->refnames, NULL,
+					     &transaction->refnames, &renamed_sources,
 					     transaction,
 					     transaction->flags & REF_TRANSACTION_FLAG_INITIAL,
 					     err);
@@ -1411,6 +1443,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
 	strbuf_release(&referent);
 	strbuf_release(&head_referent);
 	string_list_clear(&refnames_to_check, 1);
+	string_list_clear(&renamed_sources, 0);
 
 	return ret;
 }
t/helper/test-ref-store.c +36 -0
index 7ce10c28af..ec7728334c 100644
@@ -185,6 +185,41 @@ static int cmd_reflog_transaction(struct ref_store *refs, const char **argv)
 	return ret;
 }
 
+static int cmd_copy_transaction(struct ref_store *refs, const char **argv)
+{
+	struct strbuf err = STRBUF_INIT;
+	struct ref_transaction *transaction = ref_store_transaction_begin(refs, 0, &err);
+	int ret = !transaction;
+
+	while (!ret && *argv) {
+		const char *operation = *argv++;
+		const char *source = notnull(*argv++, "source");
+		const char *destination = notnull(*argv++, "destination");
+
+		if (!strcmp(operation, "rename"))
+			ret = ref_transaction_rename(transaction, source, destination, "rename", &err);
+		else if (!strcmp(operation, "copy"))
+			ret = ref_transaction_copy(transaction, source, destination, "copy", &err);
+		else if (!strcmp(operation, "update")) {
+			struct object_id oid;
+
+			if (get_oid_hex(destination, &oid))
+				die("invalid object ID: %s", destination);
+			ret = ref_transaction_update(transaction, source, &oid,
+						     NULL, NULL, NULL, 0, "update", &err);
+		} else {
+			die("unknown operation: %s", operation);
+		}
+	}
+	if (!ret)
+		ret = ref_transaction_commit(transaction, &err);
+	if (ret)
+		error("%s", err.buf);
+	ref_transaction_free(transaction);
+	strbuf_release(&err);
+	return ret;
+}
+
 static int each_ref(const struct reference *ref, void *cb_data UNUSED)
 {
 	printf("%s %s 0x%x\n", oid_to_hex(ref->oid), ref->name, ref->flags);
@@ -348,6 +383,7 @@ static struct command commands[] = {
 	{ "delete-refs", cmd_delete_refs },
 	{ "rename-ref", cmd_rename_ref },
 	{ "reflog-transaction", cmd_reflog_transaction },
+	{ "copy-transaction", cmd_copy_transaction },
 	{ "for-each-ref", cmd_for_each_ref },
 	{ "for-each-ref--exclude", cmd_for_each_ref__exclude },
 	{ "resolve-ref", cmd_resolve_ref },
t/meson.build +1 -0
index fb5266cd0d..36d47c92c4 100644
@@ -218,6 +218,7 @@ integration_tests = [
   't1421-reflog-write.sh',
   't1422-show-ref-exists.sh',
   't1423-ref-backend.sh',
+  't1424-ref-copy-transaction.sh',
   't1425-reflog-transaction.sh',
   't1430-bad-ref-name.sh',
   't1450-fsck.sh',
t/perf/p1424-ref-copy-rename.sh new file +48 -0

48 lines not on this page · raw message

t/t1424-ref-copy-transaction.sh new file +352 -0

352 lines not on this page · raw message

Signature · 1 line
2.39.3 (Apple Git-146)
#21 lore raw

PATCH v4 4/4 refs: remove backend-specific copy and rename callbacks

In reply to Maciej Ciemborowicz

Copy and rename now use the transaction API for refs and reflogs. Remove the unused callbacks, backend implementations, and their private helpers. The files backend no longer needs a shared temporary reflog name or rollback writes that reconstruct previously visible refs and logs.

---
 refs/debug.c            |  24 ---
 refs/files-backend.c    | 334 ----------------------------------------
 refs/packed-backend.c   |   2 -
 refs/refs-internal.h    |   9 --
 refs/reftable-backend.c | 287 ----------------------------------
 5 files changed, 656 deletions(-)
refs/debug.c +0 -24
index 639db0f26e..b4991f71ae 100644
@@ -143,28 +143,6 @@ static int debug_optimize_required(struct ref_store *ref_store,
 	return res;
 }
 
-static int debug_rename_ref(struct ref_store *ref_store, const char *oldref,
-			    const char *newref, const char *logmsg)
-{
-	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res = drefs->refs->be->rename_ref(drefs->refs, oldref, newref,
-					      logmsg);
-	trace_printf_key(&trace_refs, "rename_ref: %s -> %s \"%s\": %d\n", oldref, newref,
-		logmsg, res);
-	return res;
-}
-
-static int debug_copy_ref(struct ref_store *ref_store, const char *oldref,
-			  const char *newref, const char *logmsg)
-{
-	struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store;
-	int res =
-		drefs->refs->be->copy_ref(drefs->refs, oldref, newref, logmsg);
-	trace_printf_key(&trace_refs, "copy_ref: %s -> %s \"%s\": %d\n", oldref, newref,
-		logmsg, res);
-	return res;
-}
-
 struct debug_ref_iterator {
 	struct ref_iterator base;
 	struct ref_iterator *iter;
@@ -453,8 +431,6 @@ struct ref_storage_be refs_be_debug = {
 	.optimize = debug_optimize,
 	.optimize_required = debug_optimize_required,
 
-	.rename_ref = debug_rename_ref,
-	.copy_ref = debug_copy_ref,
 
 	.iterator_begin = debug_ref_iterator_begin,
 	.read_raw_ref = debug_read_raw_ref,
refs/files-backend.c +0 -334

352 lines not on this page · raw message

refs/packed-backend.c +0 -2
index a73fc6aca7..364a912912 100644
@@ -2164,8 +2164,6 @@ struct ref_storage_be refs_be_packed = {
 	.optimize = packed_optimize,
 	.optimize_required = packed_optimize_required,
 
-	.rename_ref = NULL,
-	.copy_ref = NULL,
 
 	.iterator_begin = packed_ref_iterator_begin,
 	.read_raw_ref = packed_read_raw_ref,
refs/refs-internal.h +0 -9
index 2746ce5b49..c415ba0a4e 100644
@@ -470,13 +470,6 @@ typedef int optimize_required_fn(struct ref_store *ref_store,
 				 struct refs_optimize_opts *opts,
 				 bool *required);
 
-typedef int rename_ref_fn(struct ref_store *ref_store,
-			  const char *oldref, const char *newref,
-			  const char *logmsg);
-typedef int copy_ref_fn(struct ref_store *ref_store,
-			  const char *oldref, const char *newref,
-			  const char *logmsg);
-
 /*
  * Iterate over the references in `ref_store` whose names start with
  * `prefix`. `prefix` is matched as a literal string, without regard
@@ -596,8 +589,6 @@ struct ref_storage_be {
 
 	optimize_fn *optimize;
 	optimize_required_fn *optimize_required;
-	rename_ref_fn *rename_ref;
-	copy_ref_fn *copy_ref;
 
 	ref_iterator_begin_fn *iterator_begin;
 	read_raw_ref_fn *read_raw_ref;
refs/reftable-backend.c +0 -287

299 lines not on this page · raw message

Signature · 1 line
2.39.3 (Apple Git-146)
#22 lore raw

Re: [PATCH v4 0/4] refs: run copy and rename through transactions

In reply to Maciej Ciemborowicz
Quoted 6 lines · Maciej Ciemborowicz

Changes since v3:

* Rebase onto 6de20f6092 (The 4th batch, 2026-10-06), the master commit used in Junio's report.
* Preserve the packed preparation error in patch 2 as described above.
* Register t1425 and t1424 in t/meson.build in the commits adding them.

Please engage with the reviewers. Just posting new versions without replying to them at all will very likely not get you anywhere. This kind of behaviour is nowadays a red flag and often hints at contributors who are basically just a meat proxy. And as a consequence, reviewers are very likely to disengage and stop reviewing your patch series altogether, which is frustrating to everyone involved.

Patrick

#23 lore raw

Re: [PATCH v4 0/4] refs: run copy and rename through transactions

Quoted 6 lines · Patrick Steinhardt

Please engage with the reviewers. Just posting new versions without replying to them at all will very likely not get you anywhere. This kind of behaviour is nowadays a red flag and often hints at contributors who are basically just a meat proxy. And as a consequence, reviewers are very likely to disengage and stop reviewing your patch series altogether, which is frustrating to everyone involved.

I reproduced both failures. In v3, preparing the reflogs overwrote an earlier packed-refs preparation error with success. V4 jumps to cleanup on that error.
I had not run t0600 or t5510 before submitting v3. Both now pass unchanged, and the full standard test suite passes on macOS with both files and reftable, based on 6de20f6092. Sorry for missing this before submission.

Thanks,
Maciej

#24 lore raw

Re: [PATCH v4 0/4] refs: run copy and rename through transactions

Quoted 1 line · Patrick Steinhardt

And as a consequence, reviewers are very likely to disengage and stop reviewing your patch series altogether, which is frustrating to everyone involved.

I also read Notes from the Git Contributor's Summit yesterday, so I understand the problem, and I'm curious to see how things develop. I try to respect the people reviewing code and avoid being just a "meat proxy", but I increasingly feel like I'm becoming one myself. Month after month, AI is doing more and more of my work, and it's simply better at it than I am. If I had written all of this by hand, first, it would have taken me a month, and second, I would have made far more mistakes than AI does.

I understand that the patch is large, and I'm concerned that this could be a barrier to code review and discourage people from reading it and eventually merging it. After all, the more code there is, the harder it becomes to maintain. If you don't have the time to review it, I completely understand. In that case, I'll wait for someone else to solve the problem, as I suspect it will come up again on the mailing list at some point in the future.

Thanks,
Maciej

#25 lore raw

Re: [PATCH v4 0/4] refs: run copy and rename through transactions

Quoted 3 lines · Maciej Ciemborowicz

better at it than I am. If I had written all of this by hand, first, it would have taken me a month, and second, I would have made far more mistakes than AI does.

And thanks to your learning, the next patch series you will write would be of much higher quality.

Quoted 2 lines

I understand that the patch is large, and I'm concerned that this could be a barrier to code review and discourage people from reading

Large is not a problem; unnecessarily large is.

Code generated by an LLM tends to be unnecessarily large, replete with poor refactorings and outright repetitions of existing code. This often shows that insufficient thought went into crafting the final result. Perhaps this is unavoidable, since they do not really think.

#26 lore raw

Re: [PATCH v4 0/4] refs: run copy and rename through transactions

Quoted 10 lines · Junio C Hamano

A response to reviews on the N-th round must come long before sending the v(N+1) round of patches. Some contributors send them after v(N+1), or immediately before, but the proper time to respond is soon after receiving the reviews on vN and having had enough time to understand the comments, before starting work on v(N+1). Only after that work is complete would you send the new patches. Hence, we expect the time between vN and v(N+1) from real contributors to be measured in days, not hours. Whenever I see vN responses arrive after or immediately before the v(N+1) patches, or worse, no response at all but just the new patches, it smells fishy.

I wouldn't rely on that. It's easy to fake. I'd rather ask for transparency.

Quoted 1 line

And thanks to your learning, the next patch series you will write would be of much higher quality.

TL;DR: I'm facing a moral dilemma: should I refrain from submitting a patch, or submit one written with the help of AI? If what I'm submitting isn't useful, feel free to say so or simply ignore the patch. If it has value and people are willing to accept it, I'm happy to continue.

My current workflow looks like this:

1. I give an AI agent a task.
2. I check whether the solution works. If it doesn't, I refine the instructions until it does.
3. I check for edge cases.
4. I do a code review, ask the AI agent to explain what it wrote, and have it make corrections. My own code review is pretty weak, though, because I haven't spent (and won't spend) thousands of hours working with C and Git internals.
5. I ask another AI agent to review the code and think through its findings. I usually conclude that its review is better than mine.
6. I go back to "writing" code, and the agent implements the changes suggested during the review.
7. GOTO 4 if I'm still not satisfied.

At this point, I face a moral dilemma, because the next step is to hand the patch over to other people to read, which means asking them to spend their time on it. I have the following options:

1. Report the bug without submitting a patch.
2. Report the bug and spend a month writing the patch myself, learning Git internals and C along the way. Unfortunately, I'm not planning a career in C. I've been programming for well over a decade in a completely different stack, and I simply can't afford to devote that much time to it. It might be worthwhile if I intended to spend the next several years working with Git and C, but unfortunately, that's not an option for me. On top of that, I can't shake the feeling that AI is already learning faster than I am.
3. Report the bug and solve the problem as well as I can with the help of an AI agent.

So realistically, my choice comes down to options 1 and 3. And that's my moral dilemma: I don't know whether it's better not to submit a patch at all, or to do the best I can with the time I have, using AI.

Choosing option 3, however, creates another dilemma before submitting the patch: Have I reviewed the code thoroughly enough myself? Do I understand what I'm doing well enough?

I chose option 3, and out of respect for other people's time, I'm doing my best to understand what I'm submitting.

And the only thing I can do to feel that I'm being honest about it is to be transparent and explain exactly what my workflow for developing this patch looks like.

Thanks,
Maciej

#27 lore raw

Re: [PATCH v4 0/4] refs: run copy and rename through transactions

Quoted 4 lines · Maciej Ciemborowicz

[snip]
And the only thing I can do to feel that I'm being honest about it is to be transparent and explain exactly what my workflow for developing this patch looks like.

Augment option #1 with stating upfront that one is using a coding agents and to what degree. That’s full transparancy and people can then choose to engage or not.

#28 lore raw

Re: [PATCH v4 0/4] refs: run copy and rename through transactions

Quoted 3 lines · Kristoffer Haugsbakk

Augment option #1 with stating upfront that one is using a coding agents and to what degree. That’s full transparancy and people can then choose to engage or not.

Yes, that could be a rule.

Thanks,
Maciej

#29 lore raw

Re: [PATCH v4 0/4] refs: run copy and rename through transactions

In reply to Maciej Ciemborowicz
Quoted 1 line · Maciej Ciemborowicz

On Thu, Oct 8, 2026 at 5:46 PM Junio C Hamano <[email protected]> wrote:

[snip]

Quoted 3 lines

So realistically, my choice comes down to options 1 and 3. And that's my moral dilemma: I don't know whether it's better not to submit a patch at all, or to do the best I can with the time I have, using AI.

I think one important factor here is the size and complexity of the changes that you are proposing. If the change you're about to submit is smallish and can be reasoned through quite easily then I'd always be in favor of submitting such a patch. But if it's large and complex I'd refrain from doing so because chances are high that there are lots of nuances that the AI will get wrong and that you cannot vet because you lack the context.

And I think that's something that was true even before AI. People that want to contributo to Git have a bit of a ramp-up time, and the guidance has always been to start with microprojects and then work your way up. And I think that guidance continues to apply even when these newcomers are equipped with AI.

Patrick

#30 lore raw

Re: [PATCH v4 0/4] refs: run copy and rename through transactions

In reply to Maciej Ciemborowicz
Quoted 16 lines · Maciej Ciemborowicz

At this point, I face a moral dilemma, because the next step is to hand the patch over to other people to read, which means asking them to spend their time on it. I have the following options:

1. Report the bug without submitting a patch.
2. Report the bug and spend a month writing the patch myself, learning Git internals and C along the way. Unfortunately, I'm not planning a career in C. I've been programming for well over a decade in a completely different stack, and I simply can't afford to devote that much time to it. It might be worthwhile if I intended to spend the next several years working with Git and C, but unfortunately, that's not an option for me. On top of that, I can't shake the feeling that AI is already learning faster than I am.
3. Report the bug and solve the problem as well as I can with the help of an AI agent.

Quoted 3 lines

So realistically, my choice comes down to options 1 and 3. And that's my moral dilemma: I don't know whether it's better not to submit a patch at all, or to do the best I can with the time I have, using AI.

It's better not to submit a patch at all if you can't submit something of reasonably good quality or can't satisfy the guidelines in SubmittingPatches (either with regard to AI usage or in any other way). I've been contributing for probably a decade and there are lots of times I submit a bug report or notice a problem and _don't_ submit a patch, either because I'm limited in time or because it's something that I'm just not familiar with (like the internals of the pack bitmap code). Reporting bugs is valuable to us even without a patch and nobody is expected to magically understand all the code.

The problem you reported has been noticed by other people, so it may be that someone who _is_ familiar with the code or confidence in C picks it up. That happened with a change that went into 2.56 about an issue I reported around the 2.51 time frame. I agree this is something that should ideally be fixed and a fix would be valuable for another problem that I've also reported (git remote rename is very expensive with many remote-tracking refs), so it's likely someone will pick it up eventually and that could end up being me if nobody else gets to it first (no promises, though).

Signature · 2 lines
brian m. carlson (they/them)
Toronto, Ontario, CA
#31 lore raw

Re: [PATCH v4 0/4] refs: run copy and rename through transactions

In reply to Patrick Steinhardt
Quoted 8 lines · Patrick Steinhardt
Quoted 6 lines · Maciej Ciemborowicz

Changes since v3:

* Rebase onto 6de20f6092 (The 4th batch, 2026-10-06), the master commit used in Junio's report.
* Preserve the packed preparation error in patch 2 as described above.
* Register t1425 and t1424 in t/meson.build in the commits adding them.

Quoted 6 lines

Please engage with the reviewers. Just posting new versions without replying to them at all will very likely not get you anywhere. This kind of behaviour is nowadays a red flag and often hints at contributors who are basically just a meat proxy. And as a consequence, reviewers are very likely to disengage and stop reviewing your patch series altogether, which is frustrating to everyone involved.

Thanks for bringing this up.

A response to reviews on the N-th round must come long before sending the v(N+1) round of patches. Some contributors send them after v(N+1), or immediately before, but the proper time to respond is soon after receiving the reviews on vN and having had enough time to understand the comments, before starting work on v(N+1). Only after that work is complete would you send the new patches. Hence, we expect the time between vN and v(N+1) from real contributors to be measured in days, not hours. Whenever I see vN responses arrive after or immediately before the v(N+1) patches, or worse, no response at all but just the new patches, it smells fishy.

#32 lore raw

Re: [PATCH v4 0/4] refs: run copy and rename through transactions

Quoted 19 lines · Junio C Hamano
Quoted 8 lines · Patrick Steinhardt
Quoted 6 lines · Maciej Ciemborowicz

Changes since v3:

* Rebase onto 6de20f6092 (The 4th batch, 2026-10-06), the master commit used in Junio's report.
* Preserve the packed preparation error in patch 2 as described above.
* Register t1425 and t1424 in t/meson.build in the commits adding them.

Quoted 6 lines

Please engage with the reviewers. Just posting new versions without replying to them at all will very likely not get you anywhere. This kind of behaviour is nowadays a red flag and often hints at contributors who are basically just a meat proxy. And as a consequence, reviewers are very likely to disengage and stop reviewing your patch series altogether, which is frustrating to everyone involved.

Thanks for bringing this up.

Quoted 10 lines

A response to reviews on the N-th round must come long before sending the v(N+1) round of patches. Some contributors send them after v(N+1), or immediately before, but the proper time to respond is soon after receiving the reviews on vN and having had enough time to understand the comments, before starting work on v(N+1). Only after that work is complete would you send the new patches. Hence, we expect the time between vN and v(N+1) from real contributors to be measured in days, not hours. Whenever I see vN responses arrive after or immediately before the v(N+1) patches, or worse, no response at all but just the new patches, it smells fishy.

This is kinda why I stopped reviewing the other patches [1] from the author, my reviews were simply met with N+1 version of the series. At some point I felt it would've been faster if I used a LLM locally for the same task and reviewed its code instead.

[1]: https://lore.kernel.org/git/CACQ=SRGTTdQ+dHXhN6F52dBv5KxZBRfk_Em2fvmEmGJDoB6oTg@mail.gmail.com/

#33 lore raw

Re: [PATCH v4 0/4] refs: run copy and rename through transactions

Quoted 1 line

At some point I felt it would've been faster if I used a LLM locally for

the same task and reviewed its code instead.

If writing the patches on your end would be more efficient than reviewing mine, I'd be more than happy to go with that approach. Ultimately, my main goal is simply to get both issues resolved, the one discussed here and the one in the thread you referenced. I don't want to hold you back.

Thanks,
Maciej

Quoted 39 lines · Karthik Nayak
Quoted 19 lines · Junio C Hamano
Quoted 8 lines · Patrick Steinhardt
Quoted 6 lines · Maciej Ciemborowicz

Changes since v3:

* Rebase onto 6de20f6092 (The 4th batch, 2026-10-06), the master commit used in Junio's report.
* Preserve the packed preparation error in patch 2 as described above.
* Register t1425 and t1424 in t/meson.build in the commits adding them.

Quoted 6 lines

Please engage with the reviewers. Just posting new versions without replying to them at all will very likely not get you anywhere. This kind of behaviour is nowadays a red flag and often hints at contributors who are basically just a meat proxy. And as a consequence, reviewers are very likely to disengage and stop reviewing your patch series altogether, which is frustrating to everyone involved.

Thanks for bringing this up.

Quoted 10 lines

A response to reviews on the N-th round must come long before sending the v(N+1) round of patches. Some contributors send them after v(N+1), or immediately before, but the proper time to respond is soon after receiving the reviews on vN and having had enough time to understand the comments, before starting work on v(N+1). Only after that work is complete would you send the new patches. Hence, we expect the time between vN and v(N+1) from real contributors to be measured in days, not hours. Whenever I see vN responses arrive after or immediately before the v(N+1) patches, or worse, no response at all but just the new patches, it smells fishy.

This is kinda why I stopped reviewing the other patches [1] from the author, my reviews were simply met with N+1 version of the series. At some point I felt it would've been faster if I used a LLM locally for the same task and reviewed its code instead.

[1]: https://lore.kernel.org/git/CACQ=SRGTTdQ+dHXhN6F52dBv5KxZBRfk_Em2fvmEmGJDoB6oTg@mail.gmail.com/

#34 lore raw

Re: [PATCH v4 0/3] refs: report old OIDs for batched deletions

In reply to Karthik Nayak

Hi Junio,

Quoted 2 lines

Can we avoid REF_TRANSACTION_FLAG_SKIP_HOOK by changing the call chain instead of exposing a general mechanism for skipping hooks?

I looked into that, but I do not think it removes the distinction cleanly. The packed-refs transaction is an internal, physical part of one logical files-backend update: it needs the normal transaction machinery, while the reference-transaction hook must observe only the outer logical update.

Restructuring the call chain would either duplicate the prepare/finish/abort lifecycle or hide the same hook-suppression decision in a less explicit helper. I do agree that this should not be a public escape hatch, though. I will keep it internal to the refs implementation and limit its use to these internal transactions.

Quoted 2 lines

Should the int copy parameter simply be bool copy, given that the function has only two modes?

Yes, agreed. It is a two-state mode, so bool copy expresses the contract more clearly. I will change it in the next reroll.

Thanks,
Maciej