git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 9/9] refs: reimplement refs_delete_refs() and run hook once

From
Jiang Xin <worldhello.net@gmail.com>
Date
Jul 29, 2022, 10:12 UTC
Message-ID
<20220729101245.6469-10-worldhello.net@gmail.com>
In-Reply-To
<20220729101245.6469-1-worldhello.net@gmail.com>
From: Jiang Xin <zhiyou.jx@alibaba-inc.com>

When delete references using "git branch -d" or "git tag -d", there will be duplicate call of "reference-transaction committed" for same refs. This is because "refs_delete_refs()" is called twice, once for files-backend and once for packed-backend, and we used to reinvented the wheel in "files_delete_refs()" and "packed_delete_refs()". By removing "packed_delete_refs()" and reimplement "files_delete_refs()", the "reference-transaction" hook will run only once for deleted branches and tags.

The behavior of the following git commands and the last two testcases have been fixed in t1416:

 * git branch -d <branch>
 * git tag -d <tag>

A testcase in t5510 is broken because we used to call the function "packed_refs_lock()", but it is not necessary if the deleted reference is not in the "packed-refs" file.

Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
---
 refs/files-backend.c             | 21 ++++++-------
 refs/packed-backend.c            | 51 +-------------------------------
 t/t1416-ref-transaction-hooks.sh |  4 +--
 t/t5510-fetch.sh                 | 17 +++++++++++
 4 files changed, 29 insertions(+), 64 deletions(-)
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 8baea66e58..21426efaae 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -1268,31 +1268,27 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
 static int files_delete_refs(struct ref_store *ref_store, const char *msg,
 			     struct string_list *refnames, unsigned int flags)
 {
-	struct files_ref_store *refs =
-		files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
+	struct ref_transaction *transaction;
 	struct strbuf err = STRBUF_INIT;
 	int i, result = 0;
 
 	if (!refnames->nr)
 		return 0;
 
-	if (packed_refs_lock(refs->packed_ref_store, 0, &err))
-		goto error;
-
-	if (refs_delete_refs(refs->packed_ref_store, msg, refnames, flags)) {
-		packed_refs_unlock(refs->packed_ref_store);
+	transaction = ref_store_transaction_begin(ref_store, &err);
+	if (!transaction)
 		goto error;
-	}
-
-	packed_refs_unlock(refs->packed_ref_store);
 
 	for (i = 0; i < refnames->nr; i++) {
 		const char *refname = refnames->items[i].string;
-
-		if (refs_delete_ref(&refs->base, msg, refname, NULL, flags))
+		if (ref_transaction_delete(transaction, refname, NULL,
+					   flags, msg, &err))
 			result |= error(_("could not remove reference %s"), refname);
 	}
+	if (ref_transaction_commit(transaction, &err))
+		goto error;
 
+	ref_transaction_free(transaction);
 	strbuf_release(&err);
 	return result;
 
@@ -1309,6 +1305,7 @@ static int files_delete_refs(struct ref_store *ref_store, const char *msg,
 	else
 		error(_("could not delete references: %s"), err.buf);
 
+	ref_transaction_free(transaction);
 	strbuf_release(&err);
 	return -1;
 }
diff --git a/refs/packed-backend.c b/refs/packed-backend.c
index 97b6837767..fdb7a0a52c 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
@@ -1519,55 +1519,6 @@ static int packed_initial_transaction_commit(struct ref_store *ref_store,
 	return ref_transaction_commit(transaction, err);
 }
 
-static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
-			     struct string_list *refnames, unsigned int flags)
-{
-	struct packed_ref_store *refs =
-		packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
-	struct strbuf err = STRBUF_INIT;
-	struct ref_transaction *transaction;
-	struct string_list_item *item;
-	int ret;
-
-	(void)refs; /* We need the check above, but don't use the variable */
-
-	if (!refnames->nr)
-		return 0;
-
-	/*
-	 * Since we don't check the references' old_oids, the
-	 * individual updates can't fail, so we can pack all of the
-	 * updates into a single transaction.
-	 */
-
-	transaction = ref_store_transaction_begin(ref_store, &err);
-	if (!transaction)
-		return -1;
-
-	for_each_string_list_item(item, refnames) {
-		if (ref_transaction_delete(transaction, item->string, NULL,
-					   flags, msg, &err)) {
-			warning(_("could not delete reference %s: %s"),
-				item->string, err.buf);
-			strbuf_reset(&err);
-		}
-	}
-
-	ret = ref_transaction_commit(transaction, &err);
-
-	if (ret) {
-		if (refnames->nr == 1)
-			error(_("could not delete reference %s: %s"),
-			      refnames->items[0].string, err.buf);
-		else
-			error(_("could not delete references: %s"), err.buf);
-	}
-
-	ref_transaction_free(transaction);
-	strbuf_release(&err);
-	return ret;
-}
-
 static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
 {
 	/*
@@ -1595,7 +1546,7 @@ struct ref_storage_be refs_be_packed = {
 
 	.pack_refs = packed_pack_refs,
 	.create_symref = NULL,
-	.delete_refs = packed_delete_refs,
+	.delete_refs = NULL,
 	.rename_ref = NULL,
 	.copy_ref = NULL,
 
diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh
index df75e5727c..f64166f9d7 100755
--- a/t/t1416-ref-transaction-hooks.sh
+++ b/t/t1416-ref-transaction-hooks.sh
@@ -744,7 +744,7 @@ test_expect_success "branch: rename branches" '
 	test_cmp_heads_and_tags -C workdir expect
 '
 
-test_expect_failure "branch: remove branches" '
+test_expect_success "branch: remove branches" '
 	test_when_finished "rm -f $HOOK_OUTPUT" &&
 
 	cat >expect <<-EOF &&
@@ -873,7 +873,7 @@ test_expect_success "tag: update refs to create loose refs" '
 	test_cmp_heads_and_tags -C workdir expect
 '
 
-test_expect_failure "tag: remove tags with mixed ref_stores" '
+test_expect_success "tag: remove tags with mixed ref_stores" '
 	test_when_finished "rm -f $HOOK_OUTPUT" &&
 
 	cat >expect <<-EOF &&
diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh
index b45879a760..22de7ac9ec 100755
--- a/t/t5510-fetch.sh
+++ b/t/t5510-fetch.sh
@@ -168,6 +168,8 @@ test_expect_success REFFILES 'fetch --prune fails to delete branches' '
 	cd "$D" &&
 	git clone . prune-fail &&
 	cd prune-fail &&
+	git update-ref refs/remotes/origin/extrabranch main~ &&
+	git pack-refs --all &&
 	git update-ref refs/remotes/origin/extrabranch main &&
 	: this will prevent --prune from locking packed-refs for deleting refs, but adding loose refs still succeeds  &&
 	>.git/packed-refs.new &&
@@ -175,6 +177,21 @@ test_expect_success REFFILES 'fetch --prune fails to delete branches' '
 	test_must_fail git fetch --prune origin
 '
 
+test_expect_success REFFILES 'fetch --prune ok for loose refs not in locked packed-refs' '
+	test_when_finished "cd \"$D\"; rm -rf \"prune-ok-ref-not-packed\"" &&
+	cd "$D" &&
+	git clone . prune-ok-ref-not-packed &&
+	(
+		cd prune-ok-ref-not-packed &&
+		git update-ref refs/remotes/origin/extrabranch main &&
+		: for loose refs not in packed-refs, we can delete them even the packed-refs is locked &&
+		:>.git/packed-refs.new &&
+
+		git fetch --prune origin &&
+		test_must_fail git rev-parse refs/remotes/origin/extrabranch --
+	)
+'
+
 test_expect_success 'fetch --atomic works with a single branch' '
 	test_when_finished "rm -rf \"$D\"/atomic" &&
 
-- 
2.36.1.25.gc87d5ad63a.dirty
Previous: Jiang XinNext: Michael Heemskerk
Message 25 of 27 in “Fix issues of reference-transaction hook for various git commands”
  1. 0/9 Fix issues of reference-transaction hook for various git commandsJiang Xin, Jul 29, 2022
  2. 2/9 refs: update missing old-oid in transaction from lockfileJiang Xin, Jul 29, 2022
  3. 3/9 refs: add new field in transaction for running transaction hookJiang Xin, Jul 29, 2022
  4. 1/9 t1416: more testcases for reference-transaction hookJiang Xin, Jul 29, 2022
  5. Eric SunshineJul 30, 2022
  6. Jiang XinJul 31, 2022
  7. 4/9 refs: do not run transaction hook for git-pack-refsJiang Xin, Jul 29, 2022
  8. 5/9 refs: avoid duplicate running of the reference-transaction hookJiang Xin, Jul 29, 2022
  9. Michael HeemskerkAug 2, 2022
  10. Jiang XinAug 5, 2022
  11. 0/9 Fix issues of refx-txn hook for various git commandsJiang Xin, Aug 19, 2022
  12. 2/9 refs: update missing old-oid in transaction from lockfileJiang Xin, Aug 19, 2022
  13. 3/9 refs: add new field in transaction for running transaction hookJiang Xin, Aug 19, 2022
  14. 1/9 t1416: more testcases for reference-transaction hookJiang Xin, Aug 19, 2022
  15. 4/9 refs: do not run transaction hook for git-pack-refsJiang Xin, Aug 19, 2022
  16. 5/9 refs: avoid duplicate running of the reference-transaction hookJiang Xin, Aug 19, 2022
  17. 6/9 refs: add reflog_info to hold more fields for reflog entryJiang Xin, Aug 19, 2022
  18. 9/9 refs: reimplement refs_delete_refs() and run hook onceJiang Xin, Aug 19, 2022
  19. 7/9 refs: get error message via refs_update_ref_extended()Jiang Xin, Aug 19, 2022
  20. 8/9 refs: reimplement files_copy_or_rename_ref() to run refs-txn hookJiang Xin, Aug 19, 2022
  21. 6/9 refs: add reflog_info to hold more fields for reflog entryJiang Xin, Jul 29, 2022
  22. Jiang XinAug 1, 2022
  23. 7/9 refs: get error message via refs_update_ref_extended()Jiang Xin, Jul 29, 2022
  24. 8/9 refs: reimplement files_copy_or_rename_ref() to run hookJiang Xin, Jul 29, 2022
  25. 9/9 refs: reimplement refs_delete_refs() and run hook onceJiang Xin, Jul 29, 2022
  26. Michael HeemskerkAug 2, 2022
  27. Patrick SteinhardtAug 9, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.