[PATCH 4/4] hook: allow out-of-repo 'git hook' invocations
- From
Adrian Ratiu <adrian.ratiu@collabora.com>
- Date
- Feb 4, 2026, 16:51 UTC
- Message-ID
- <20260204165126.1548805-5-adrian.ratiu@collabora.com>
- In-Reply-To
- <20260204165126.1548805-1-adrian.ratiu@collabora.com>
From: Emily Shaffer <emilyshaffer@google.com>
Since hooks can now be supplied via the config, and a config can be present without a gitdir via the global and system configs, we can start to allow 'git hook run' to occur without a gitdir. This enables us to do things like run sendemail-validate hooks when running 'git send-email' from a nongit directory.
It still doesn't make sense to look for hooks in the hookdir in nongit repos, though, as there is no hookdir.
Signed-off-by: Emily Shaffer <emilyshaffer@google.com> --- git.c | 2 +- t/t1800-hook.sh | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/git.c b/git.c index c5fad56813..a9e462ee32 100644 --- a/git.c +++ b/git.c @@ -586,7 +586,7 @@ static struct cmd_struct commands[] = { { "grep", cmd_grep, RUN_SETUP_GENTLY }, { "hash-object", cmd_hash_object }, { "help", cmd_help }, - { "hook", cmd_hook, RUN_SETUP }, + { "hook", cmd_hook, RUN_SETUP_GENTLY }, { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT }, { "init", cmd_init_db }, { "init-db", cmd_init_db }, diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh index bc4862e982..21ff6a68f0 100755 --- a/t/t1800-hook.sh +++ b/t/t1800-hook.sh @@ -102,12 +102,18 @@ test_expect_success 'git hook run -- pass arguments' ' test_cmp expect actual ' -test_expect_success 'git hook run -- out-of-repo runs excluded' ' - test_hook test-hook <<-EOF && - echo Test hook - EOF +test_expect_success 'git hook run: out-of-repo runs execute global hooks' ' + test_config_global hook.global-hook.event test-hook --add && + test_config_global hook.global-hook.command "echo no repo no problems" --add && - nongit test_must_fail git hook run test-hook + echo "global-hook" >expect && + nongit git hook list test-hook >actual && + test_cmp expect actual && + + echo "no repo no problems" >expect && + + nongit git hook run test-hook 2>actual && + test_cmp expect actual ' test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
-- 2.52.0.732.gb351b5166d.dirty