]> git.ipfire.org Git - thirdparty/git.git/commitdiff
hook: show config scope in git hook list
authorAdrian Ratiu <adrian.ratiu@collabora.com>
Wed, 25 Mar 2026 19:55:01 +0000 (21:55 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 25 Mar 2026 21:00:47 +0000 (14:00 -0700)
Users running "git hook list" can see which hooks are configured but
have no way to tell at which config scope (local, global, system...)
each hook was defined.

Store the scope from ctx->kvi->scope in the single-pass config callback,
then carry it through the cache to the hook structs, so we can expose it
to users via the "git hook list --show-scope" flag, which mirrors the
existing git config --show-scope convention.

Without the flag the output is unchanged.

The scope is printed as a tab-separated prefix (like "git config --show-scope"),
making it unambiguously machine-parseable even when the friendly name
contains spaces.

Example usage:
$ git hook list --show-scope pre-commit
global linter
local no-leaks
hook from hookdir

Traditional hooks from the hookdir are unaffected by --show-scope since
the config scope concept does not apply to them.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-hook.adoc
builtin/hook.c
hook.c
hook.h
t/t1800-hook.sh

index 966388660aea4070e8dfc475f67cbaafa88d4d8f..e7d399ae57af54a21fbec2f0a8b3ba14578a0ec6 100644 (file)
@@ -9,7 +9,7 @@ SYNOPSIS
 --------
 [verse]
 'git hook' run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]
-'git hook' list [-z] <hook-name>
+'git hook' list [-z] [--show-scope] <hook-name>
 
 DESCRIPTION
 -----------
@@ -113,7 +113,7 @@ Any positional arguments to the hook should be passed after a
 mandatory `--` (or `--end-of-options`, see linkgit:gitcli[7]). See
 linkgit:githooks[5] for arguments hooks might expect (if any).
 
-list [-z]::
+list [-z] [--show-scope]::
        Print a list of hooks which will be run on `<hook-name>` event. If no
        hooks are configured for that event, print a warning and return 1.
        Use `-z` to terminate output lines with NUL instead of newlines.
@@ -134,6 +134,12 @@ OPTIONS
 -z::
        Terminate "list" output lines with NUL instead of newlines.
 
+--show-scope::
+       For "list"; prefix each configured hook's friendly name with a
+       tab-separated config scope (e.g. `local`, `global`, `system`),
+       mirroring the output style of `git config --show-scope`. Traditional
+       hooks from the hookdir are unaffected.
+
 WRAPPERS
 --------
 
index 54b737990b070cab205f8abe6d30aaad468c9b18..4cc65a0dc5970c0a0a4cb7db7a2a1caa3c8e79e9 100644 (file)
@@ -9,7 +9,7 @@
 #define BUILTIN_HOOK_RUN_USAGE \
        N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
 #define BUILTIN_HOOK_LIST_USAGE \
-       N_("git hook list [-z] <hook-name>")
+       N_("git hook list [-z] [--show-scope] <hook-name>")
 
 static const char * const builtin_hook_usage[] = {
        BUILTIN_HOOK_RUN_USAGE,
@@ -33,11 +33,14 @@ static int list(int argc, const char **argv, const char *prefix,
        struct string_list_item *item;
        const char *hookname = NULL;
        int line_terminator = '\n';
+       int show_scope = 0;
        int ret = 0;
 
        struct option list_options[] = {
                OPT_SET_INT('z', NULL, &line_terminator,
                            N_("use NUL as line terminator"), '\0'),
+               OPT_BOOL(0, "show-scope", &show_scope,
+                        N_("show the config scope that defined each hook")),
                OPT_END(),
        };
 
@@ -70,7 +73,14 @@ static int list(int argc, const char **argv, const char *prefix,
                        printf("%s%c", _("hook from hookdir"), line_terminator);
                        break;
                case HOOK_CONFIGURED:
-                       printf("%s%c", h->u.configured.friendly_name, line_terminator);
+                       if (show_scope)
+                               printf("%s\t%s%c",
+                                      config_scope_name(h->u.configured.scope),
+                                      h->u.configured.friendly_name,
+                                      line_terminator);
+                       else
+                               printf("%s%c", h->u.configured.friendly_name,
+                                      line_terminator);
                        break;
                default:
                        BUG("unknown hook kind");
diff --git a/hook.c b/hook.c
index 54f99f4989f46bc90b48b942c3c9d01a0b401f2d..74f5a1df35ace378a27060ad96f9912906ef39bc 100644 (file)
--- a/hook.c
+++ b/hook.c
@@ -110,11 +110,11 @@ static void list_hooks_add_default(struct repository *r, const char *hookname,
 
 /*
  * Cache entry stored as the .util pointer of string_list items inside the
- * hook config cache. For now carries only the command for the hook. Next
- * commits will add more data.
+ * hook config cache.
  */
 struct hook_config_cache_entry {
        char *command;
+       enum config_scope scope;
 };
 
 /*
@@ -131,7 +131,7 @@ struct hook_all_config_cb {
 
 /* repo_config() callback that collects all hook.* configuration in one pass. */
 static int hook_config_lookup_all(const char *key, const char *value,
-                                 const struct config_context *ctx UNUSED,
+                                 const struct config_context *ctx,
                                  void *cb_data)
 {
        struct hook_all_config_cb *data = cb_data;
@@ -168,7 +168,19 @@ static int hook_config_lookup_all(const char *key, const char *value,
 
                        /* Re-insert if necessary to preserve last-seen order. */
                        unsorted_string_list_remove(hooks, hook_name, 0);
-                       string_list_append(hooks, hook_name);
+
+                       if (!ctx->kvi)
+                               BUG("hook config callback called without key-value info");
+
+                       /*
+                        * Stash the config scope in the util pointer for
+                        * later retrieval in build_hook_config_map(). This
+                        * intermediate struct is transient and never leaves
+                        * that function, so we pack the enum value into the
+                        * pointer rather than heap-allocating a wrapper.
+                        */
+                       string_list_append(hooks, hook_name)->util =
+                               (void *)(uintptr_t)ctx->kvi->scope;
                }
        } else if (!strcmp(subkey, "command")) {
                /* Store command overwriting the old value */
@@ -246,6 +258,8 @@ static void build_hook_config_map(struct repository *r, struct strmap *cache)
 
                for (size_t i = 0; i < hook_names->nr; i++) {
                        const char *hname = hook_names->items[i].string;
+                       enum config_scope scope =
+                               (enum config_scope)(uintptr_t)hook_names->items[i].util;
                        struct hook_config_cache_entry *entry;
                        char *command;
 
@@ -263,6 +277,7 @@ static void build_hook_config_map(struct repository *r, struct strmap *cache)
                        /* util stores a cache entry; owned by the cache. */
                        CALLOC_ARRAY(entry, 1);
                        entry->command = xstrdup(command);
+                       entry->scope = scope;
                        string_list_append(hooks, hname)->util = entry;
                }
 
@@ -344,6 +359,7 @@ static void list_hooks_add_configured(struct repository *r,
                hook->kind = HOOK_CONFIGURED;
                hook->u.configured.friendly_name = xstrdup(friendly_name);
                hook->u.configured.command = xstrdup(entry->command);
+               hook->u.configured.scope = entry->scope;
 
                string_list_append(list, friendly_name)->util = hook;
        }
diff --git a/hook.h b/hook.h
index d2cf59e64941fd4b80f795648f98fd3f8fcd0a6e..a0432e8307e3968bc73145eb589b1f5246332155 100644 (file)
--- a/hook.h
+++ b/hook.h
@@ -1,5 +1,6 @@
 #ifndef HOOK_H
 #define HOOK_H
+#include "config.h"
 #include "run-command.h"
 #include "string-list.h"
 #include "strmap.h"
@@ -29,6 +30,7 @@ struct hook {
                struct {
                        const char *friendly_name;
                        const char *command;
+                       enum config_scope scope;
                } configured;
        } u;
 
index 7eee84fc3956c2f9d73752b1977a8a841b57b107..6fc6603da820869ec0fd2edfc87aedfaa3bba704 100755 (executable)
@@ -408,6 +408,27 @@ test_expect_success 'configured hooks run before hookdir hook' '
        test_cmp expected actual
 '
 
+test_expect_success 'git hook list --show-scope shows config scope' '
+       setup_hookdir &&
+       test_config_global hook.global-hook.command "echo global" &&
+       test_config_global hook.global-hook.event pre-commit --add &&
+       test_config hook.local-hook.command "echo local" &&
+       test_config hook.local-hook.event pre-commit --add &&
+
+       cat >expected <<-\EOF &&
+       global  global-hook
+       local   local-hook
+       hook from hookdir
+       EOF
+       git hook list --show-scope pre-commit >actual &&
+       test_cmp expected actual &&
+
+       # without --show-scope the scope must not appear
+       git hook list pre-commit >actual &&
+       test_grep ! "^global    " actual &&
+       test_grep ! "^local     " actual
+'
+
 test_expect_success 'git hook run a hook with a bad shebang' '
        test_when_finished "rm -rf bad-hooks" &&
        mkdir bad-hooks &&