@@ -538,7 +538,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 },
@@ -181,7 +181,7 @@ struct list_head *list_hooks(const char *hookname)
/* Add the hook from the hookdir. The placeholder makes it easier to
* allocate work in pick_next_hook. */
- if (find_hook(hookname))
+ if (have_git_dir() && find_hook(hookname))
append_or_move_hook(hook_head, NULL);
return hook_head;
@@ -128,15 +128,25 @@ test_expect_success 'git hook list: existing hook' '
test_cmp expect actual
'
-test_expect_success 'git hook run -- out-of-repo runs excluded' '
- write_script .git/hooks/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 &&
+
+ echo "global-hook" >expect &&
+ nongit git hook list test-hook >actual &&
+ test_cmp expect actual &&
+
+ echo "no repo no problems" >expect &&
- nongit test_must_fail git hook run test-hook
+ nongit git hook run test-hook 2>actual &&
+ test_cmp expect actual
'
test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
+ write_script .git/hooks/test-hook <<-EOF &&
+ echo Test hook
+ EOF
+
mkdir my-hooks &&
write_script my-hooks/test-hook <<-\EOF &&
echo Hook ran $1 >>actual