git/list[1] front-page[2] threads[3] people[4] search[5] about
wed 2026-10-07 16:57 UTC

[PATCH 09/10] hook: show config scope in git hook list

From
Adrian Ratiu <adrian.ratiu@collabora.com>
Date
Mar 9, 2026, 00:54 UTC
Message-ID
<20260309005416.2760030-10-adrian.ratiu@collabora.com>
In-Reply-To
<20260309005416.2760030-1-adrian.ratiu@collabora.com>

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.

Example usage: $ git hook list --show-scope pre-commit linter (global) no-leaks (local) 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>
---
 Documentation/git-hook.adoc |  9 +++++++--
 builtin/hook.c              | 14 ++++++++++++--
 hook.c                      | 24 ++++++++++++++++++++----
 hook.h                      |  2 ++
 t/t1800-hook.sh             | 19 +++++++++++++++++++
 5 files changed, 60 insertions(+), 8 deletions(-)
diff --git a/Documentation/git-hook.adoc b/Documentation/git-hook.adoc
index 966388660a..4d4e728327 100644
--- a/Documentation/git-hook.adoc
+++ b/Documentation/git-hook.adoc
@@ -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,11 @@ OPTIONS
 -z::
 	Terminate "list" output lines with NUL instead of newlines.
 
+--show-scope::
+	For "list"; print the config scope (e.g. `local`, `global`, `system`)
+	in parentheses after the friendly name of each configured hook, to show
+	where it was defined. Traditional hooks from the hookdir are unaffected.
+
 WRAPPERS
 --------
 
diff --git a/builtin/hook.c b/builtin/hook.c
index 8fc647a4de..c806640361 100644
--- a/builtin/hook.c
+++ b/builtin/hook.c
@@ -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 (%s)%c",
+				       h->u.configured.friendly_name,
+				       config_scope_name(h->u.configured.scope),
+				       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 4fe50aa38c..2c03baeaac 100644
--- a/hook.c
+++ b/hook.c
@@ -114,11 +114,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;
 };
 
 /*
@@ -135,7 +135,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;
@@ -172,7 +172,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 */
@@ -251,6 +263,8 @@ static void build_hook_config_map(struct repository *r,
 
 		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;
 
@@ -268,6 +282,7 @@ static void build_hook_config_map(struct repository *r,
 			/* 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;
 		}
 
@@ -348,6 +363,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 4d0c22f1dc..0d711ed21a 100644
--- a/hook.h
+++ b/hook.h
@@ -1,5 +1,6 @@
 #ifndef HOOK_H
 #define HOOK_H
+#include "config.h"
 #include "strvec.h"
 #include "run-command.h"
 #include "string-list.h"
@@ -29,6 +30,7 @@ struct hook {
 		struct {
 			const char *friendly_name;
 			const char *command;
+			enum config_scope scope;
 		} configured;
 	} u;
 
diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh
index 7eee84fc39..aed07575e3 100755
--- a/t/t1800-hook.sh
+++ b/t/t1800-hook.sh
@@ -408,6 +408,25 @@ test_expect_success 'configured hooks run before hookdir hook' '
 	test_cmp expected actual
 '
 
+test_expect_success 'git hook list --show-scope shows config scope' '
+	test_config_global hook.global-hook.command "echo global" &&
+	test_config_global hook.global-hook.event test-hook --add &&
+	test_config hook.local-hook.command "echo local" &&
+	test_config hook.local-hook.event test-hook --add &&
+
+	cat >expected <<-\EOF &&
+	global-hook (global)
+	local-hook (local)
+	EOF
+	git hook list --show-scope test-hook >actual &&
+	test_cmp expected actual &&
+
+	# without --show-scope the scope must not appear
+	git hook list test-hook >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 &&
-- 
2.52.0.732.gb351b5166d.dirty
Previous: Adrian RatiuNext: Adrian Ratiu
Message 10 of 71 in “config-hook cleanups and two small 'git hook list' features”
  1. 00/10 config-hook cleanups and two small 'git hook list' featuresAdrian Ratiu, Mar 9, 2026
  2. 01/10 hook: move unsorted_string_list_remove() to string-list.[ch]Adrian Ratiu, Mar 9, 2026
  3. 02/10 hook: fix minor style issuesAdrian Ratiu, Mar 9, 2026
  4. 03/10 hook: rename cb_data_free/alloc -> hook_data_free/allocAdrian Ratiu, Mar 9, 2026
  5. 04/10 hook: detect & emit two more bugsAdrian Ratiu, Mar 9, 2026
  6. 05/10 hook: replace hook_list_clear() -> string_list_clear_func()Adrian Ratiu, Mar 9, 2026
  7. 06/10 hook: make consistent use of friendly-name in docsAdrian Ratiu, Mar 9, 2026
  8. 08/10 hook: refactor hook_config_cache from strmap to named structAdrian Ratiu, Mar 9, 2026
  9. 07/10 t1800: add test to verify hook execution orderingAdrian Ratiu, Mar 9, 2026
  10. 09/10 hook: show config scope in git hook listAdrian Ratiu, Mar 9, 2026
  11. 10/10 hook: show disabled hooks in "git hook list"Adrian Ratiu, Mar 9, 2026
  12. Eric SunshineMar 9, 2026
  13. Eric SunshineMar 9, 2026
  14. Junio C HamanoMar 9, 2026
  15. Junio C HamanoMar 9, 2026
  16. Junio C HamanoMar 9, 2026
  17. Junio C HamanoMar 9, 2026
  18. Adrian RatiuMar 10, 2026
  19. Adrian RatiuMar 10, 2026
  20. Adrian RatiuMar 10, 2026
  21. Adrian RatiuMar 10, 2026
  22. SZEDER GáborMar 10, 2026
  23. Patrick SteinhardtMar 11, 2026
  24. Patrick SteinhardtMar 11, 2026
  25. Patrick SteinhardtMar 11, 2026
  26. Adrian RatiuMar 11, 2026
  27. Adrian RatiuMar 11, 2026
  28. Adrian RatiuMar 11, 2026
  29. Adrian RatiuMar 11, 2026
  30. Patrick SteinhardtMar 11, 2026
  31. 00/10 config-hook cleanups and two small 'git hook list' featuresAdrian Ratiu, Mar 20, 2026
  32. 01/10 hook: move unsorted_string_list_remove() to string-list.[ch]Adrian Ratiu, Mar 20, 2026
  33. 03/10 hook: rename cb_data_free/alloc -> hook_data_free/allocAdrian Ratiu, Mar 20, 2026
  34. 04/10 hook: detect & emit two more bugsAdrian Ratiu, Mar 20, 2026
  35. 05/10 hook: replace hook_list_clear() -> string_list_clear_func()Adrian Ratiu, Mar 20, 2026
  36. 06/10 hook: make consistent use of friendly-name in docsAdrian Ratiu, Mar 20, 2026
  37. 07/10 t1800: add test to verify hook execution orderingAdrian Ratiu, Mar 20, 2026
  38. 08/10 hook: introduce hook_config_cache_entry for per-hook dataAdrian Ratiu, Mar 20, 2026
  39. 02/10 hook: fix minor style issuesAdrian Ratiu, Mar 20, 2026
  40. 09/10 hook: show config scope in git hook listAdrian Ratiu, Mar 20, 2026
  41. 10/10 hook: show disabled hooks in "git hook list"Adrian Ratiu, Mar 20, 2026
  42. Junio C HamanoMar 23, 2026
  43. Patrick SteinhardtMar 24, 2026
  44. Patrick SteinhardtMar 24, 2026
  45. Patrick SteinhardtMar 24, 2026
  46. Patrick SteinhardtMar 24, 2026
  47. Patrick SteinhardtMar 24, 2026
  48. Junio C HamanoMar 24, 2026
  49. Adrian RatiuMar 24, 2026
  50. Adrian RatiuMar 24, 2026
  51. Adrian RatiuMar 24, 2026
  52. Adrian RatiuMar 24, 2026
  53. Patrick SteinhardtMar 25, 2026
  54. Adrian RatiuMar 25, 2026
  55. 00/12 config-hook cleanups and three small git-hook featuresAdrian Ratiu, Mar 25, 2026
  56. 01/12 hook: move unsorted_string_list_remove() to string-list.[ch]Adrian Ratiu, Mar 25, 2026
  57. 02/12 builtin/receive-pack: properly init receive_hook strbufAdrian Ratiu, Mar 25, 2026
  58. 04/12 hook: rename cb_data_free/alloc -> hook_data_free/allocAdrian Ratiu, Mar 25, 2026
  59. 03/12 hook: fix minor style issuesAdrian Ratiu, Mar 25, 2026
  60. 05/12 hook: detect & emit two more bugsAdrian Ratiu, Mar 25, 2026
  61. 06/12 hook: replace hook_list_clear() -> string_list_clear_func()Adrian Ratiu, Mar 25, 2026
  62. 07/12 hook: make consistent use of friendly-name in docsAdrian Ratiu, Mar 25, 2026
  63. 08/12 t1800: add test to verify hook execution orderingAdrian Ratiu, Mar 25, 2026
  64. 09/12 hook: introduce hook_config_cache_entry for per-hook dataAdrian Ratiu, Mar 25, 2026
  65. 10/12 hook: show config scope in git hook listAdrian Ratiu, Mar 25, 2026
  66. 11/12 hook: show disabled hooks in "git hook list"Adrian Ratiu, Mar 25, 2026
  67. 12/12 hook: reject unknown hook names in git-hook(1)Adrian Ratiu, Mar 25, 2026
  68. Junio C HamanoMar 25, 2026
  69. Adrian RatiuMar 26, 2026
  70. Patrick SteinhardtMar 27, 2026
  71. Junio C HamanoMar 27, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.