receive-pack: fix updateInstead with core.worktree

Before a8cc594333 (hooks: fix an obscure TOCTOU "did we just run a hook?" race, 2022-03-07), when receive.denyCurrentBranch is set to updateInstead, only one of push_to_checkout() or push_to_deploy() was called. That commit changed to always call push_to_checkout(), and then to call push_to_deploy() if push_to_checkout() didn't run anything. This change didn't take into account that push_to_checkout() had a side effect of modifying env, and that modified env broke updating the worktree in push_to_deploy() if core.worktree was configured. To fix this, only mutate the environment used inside push_to_commit(), rather than the environment that might later be passed to push_to_deploy(). Signed-off-by: Alyssa Ross <hi@alyssa.is> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alyssa Ross committed May 25, 2026 at 18:23 UTC 44d04e442683b53ed618b74861f056f93fcbd783
2 files changed +12 -1
builtin/receive-pack.c
+1 -1
@@ -1427,8 +1427,8 @@ static const char *push_to_checkout(unsigned char *hash,
1427 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
1428 opt.invoked_hook = invoked_hook;
1429
1430 - strvec_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
1430 strvec_pushv(&opt.env, env->v);
1431 + strvec_pushf(&opt.env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
1432 strvec_push(&opt.args, hash_to_hex(hash));
1433 if (run_hooks_opt(the_repository, push_to_checkout_hook, &opt))
1434 return "push-to-checkout hook declined";
t/t5516-fetch-push.sh
+11
@@ -1791,6 +1791,17 @@ test_expect_success 'updateInstead with push-to-checkout hook' '
1791 )
1792 '
1793
1794 +test_expect_success 'denyCurrentBranch and core.worktree' '
1795 + test_when_finished "rm -fr cloned cloned.git" &&
1796 + git clone --separate-git-dir cloned.git . cloned &&
1797 + git --git-dir cloned.git config receive.denyCurrentBranch updateInstead &&
1798 + git --git-dir cloned.git config core.worktree "$PWD/cloned" &&
1799 + test_commit raspberry &&
1800 + git push cloned.git HEAD:main &&
1801 + test_path_exists cloned/raspberry.t &&
1802 + test_must_fail git push --delete cloned.git main
1803 +'
1804 +
1805 test_expect_success 'denyCurrentBranch and worktrees' '
1806 git worktree add new-wt &&
1807 git clone . cloned &&