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

[PATCH 4/9] refs: do not run transaction hook for git-pack-refs

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

git-pack-refs will call "files_pack_refs()" to pack loose references into the "packed-refs" file, and there are no real changes to the repository. Therefore, by initializing a transaction with an empty "hook_flags" field, the "reference-transaction" hook will not run.

The "prune_refs()" and "prune_ref()" functions are called from "file_pack_refs()", and they are used to prune loose refereces which have already been packed into "packed-refs". The transaction instance in "prune_ref()" should also be initialized with an empty "hook_flags" field.

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

 * git gc
 * git pack-refs
Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
---
 refs/files-backend.c             | 10 ++++++++--
 t/t1416-ref-transaction-hooks.sh |  6 +++---
 2 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 957ebe08c0..3be317d524 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -1136,7 +1136,8 @@ static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
 	if (check_refname_format(r->name, 0))
 		return;
 
-	transaction = ref_store_transaction_begin(&refs->base, &err);
+	/* Called by "files_pack_refs()" and should not run any hooks. */
+	transaction = ref_store_transaction_begin_extended(&refs->base, 0, &err);
 	if (!transaction)
 		goto cleanup;
 	ref_transaction_add_update(
@@ -1207,7 +1208,12 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
 	struct strbuf err = STRBUF_INIT;
 	struct ref_transaction *transaction;
 
-	transaction = ref_store_transaction_begin(refs->packed_ref_store, &err);
+	/*
+	 * No real changes have occurred to the repository and no hooks
+	 * should be run for this transaction.
+	 */
+	transaction = ref_store_transaction_begin_extended(refs->packed_ref_store,
+							   0, &err);
 	if (!transaction)
 		return -1;
 
diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh
index eef7489b5b..7f8fd012e8 100755
--- a/t/t1416-ref-transaction-hooks.sh
+++ b/t/t1416-ref-transaction-hooks.sh
@@ -358,7 +358,7 @@ test_expect_success "update-ref: update HEAD, a symbolic-ref" '
 	test_cmp_heads_and_tags -C workdir expect
 '
 
-test_expect_failure "update-ref: call git-pack-refs to create packed_ref_store" '
+test_expect_success "update-ref: call git-pack-refs to create packed_ref_store" '
 	test_when_finished "rm -f $HOOK_OUTPUT" &&
 	git -C workdir pack-refs --all &&
 	test_path_is_file workdir/.git/packed-refs &&
@@ -632,7 +632,7 @@ test_expect_success "branch: create new branches" '
 	test_cmp_heads_and_tags -C workdir expect
 '
 
-test_expect_failure "branch: call git-gc to create packed_ref_store" '
+test_expect_success "branch: call git-gc to create packed_ref_store" '
 	test_when_finished "rm -f $HOOK_OUTPUT" &&
 	git -C workdir gc &&
 	test_path_is_file workdir/.git/packed-refs &&
@@ -835,7 +835,7 @@ test_expect_success "tag: create new tags" '
 	test_cmp_heads_and_tags -C workdir expect
 '
 
-test_expect_failure "tag: call git-pack-refs to create packed_ref_store" '
+test_expect_success "tag: call git-pack-refs to create packed_ref_store" '
 	test_when_finished "rm -f $HOOK_OUTPUT" &&
 	git -C workdir pack-refs --all &&
 	test_path_is_file workdir/.git/packed-refs &&
-- 
2.36.1.25.gc87d5ad63a.dirty
Previous: Jiang XinNext: Jiang Xin
Message 7 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.