Skip to content

Commit e5256c8

Browse files
pks-tgitster
authored andcommitted
refs: fix interleaving hook calls with reference-transaction hook
In order to not repeatedly search for the reference-transaction hook in case it's getting called multiple times, we use a caching mechanism to only call `find_hook()` once. What was missed though is that the return value of `find_hook()` actually comes from a static strbuf, which means it will get overwritten when calling `find_hook()` again. As a result, we may call the wrong hook with parameters of the reference-transaction hook. This scenario was spotted in the wild when executing a git-push(1) with multiple references, where there are interleaving calls to both the update and the reference-transaction hook. While initial calls to the reference-transaction hook work as expected, it will stop working after the next invocation of the update hook. The result is that we now start calling the update hook with parameters and stdin of the reference-transaction hook. This commit fixes the issue by storing a copy of `find_hook()`'s return value in the cache. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6c18d03 commit e5256c8

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

refs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2001,7 +2001,7 @@ static int run_transaction_hook(struct ref_transaction *transaction,
20012001
if (hook == &hook_not_found)
20022002
return ret;
20032003
if (!hook)
2004-
hook = find_hook("reference-transaction");
2004+
hook = xstrdup_or_null(find_hook("reference-transaction"));
20052005
if (!hook) {
20062006
hook = &hook_not_found;
20072007
return ret;

t/t1416-ref-transaction-hooks.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,30 @@ test_expect_success 'hook gets all queued updates in aborted state' '
106106
test_cmp expect actual
107107
'
108108

109+
test_expect_success 'interleaving hook calls succeed' '
110+
test_when_finished "rm -r target-repo.git" &&
111+
112+
git init --bare target-repo.git &&
113+
114+
write_script target-repo.git/hooks/reference-transaction <<-\EOF &&
115+
echo $0 "$@" >>actual
116+
EOF
117+
118+
write_script target-repo.git/hooks/update <<-\EOF &&
119+
echo $0 "$@" >>actual
120+
EOF
121+
122+
cat >expect <<-EOF &&
123+
hooks/update refs/tags/PRE 0000000000000000000000000000000000000000 63ac8e7bcdb882293465435909f54a96de17d4f7
124+
hooks/reference-transaction prepared
125+
hooks/reference-transaction committed
126+
hooks/update refs/tags/POST 0000000000000000000000000000000000000000 99d53161c3a0a903b6561b9f6c0c665b3a476401
127+
hooks/reference-transaction prepared
128+
hooks/reference-transaction committed
129+
EOF
130+
131+
git push ./target-repo.git PRE POST &&
132+
test_cmp expect target-repo.git/actual
133+
'
134+
109135
test_done

0 commit comments

Comments
 (0)