git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 11/12] hook: store and display scope for configured hooks in git hook list

From
Adrian Ratiu <adrian.ratiu@collabora.com>
Date
Mar 1, 2026, 18:44 UTC
Message-ID
<20260301184500.1488433-12-adrian.ratiu@collabora.com>
In-Reply-To
<20260301184500.1488433-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, then expose it to the 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.

$ 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                      | 17 +++++++++++++----
 hook.h                      |  2 ++
 t/t1800-hook.sh             | 19 +++++++++++++++++++
 5 files changed, 53 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 542183795a..eb8dfcd31b 100644
--- a/builtin/hook.c
+++ b/builtin/hook.c
@@ -10,7 +10,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,
@@ -34,11 +34,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(),
 	};
 
@@ -71,7 +74,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 0bab491953..a868d8cf17 100644
--- a/hook.c
+++ b/hook.c
@@ -106,11 +106,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;
 };
 
 /*
@@ -127,7 +127,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;
@@ -164,7 +164,12 @@ 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);
+			/*
+			 * Store the config scope in util so callers can
+			 * report where each hook was defined.
+			 */
+			string_list_append(hooks, hook_name)->util =
+				(void *)(uintptr_t)ctx->kvi->scope;
 		}
 	} else if (!strcmp(subkey, "command")) {
 		/* Store command overwriting the old value */
@@ -243,6 +248,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;
 
@@ -260,6 +267,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;
 		}
 
@@ -336,6 +344,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 277390b744..b862cde01b 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 0a4b2a9978..6e36ac5229 100755
--- a/t/t1800-hook.sh
+++ b/t/t1800-hook.sh
@@ -406,6 +406,25 @@ test_expect_success 'globally disabled hook can be re-enabled locally' '
 	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 67 of 71 in “Specify hooks via configs”
  1. 0/4 Specify hooks via configsAdrian Ratiu, Feb 4, 2026
  2. 2/4 hook: introduce "git hook list"Adrian Ratiu, Feb 4, 2026
  3. Patrick SteinhardtFeb 9, 2026
  4. Adrian RatiuFeb 9, 2026
  5. 3/4 hook: include hooks from the configAdrian Ratiu, Feb 4, 2026
  6. Patrick SteinhardtFeb 9, 2026
  7. Adrian RatiuFeb 9, 2026
  8. Patrick SteinhardtFeb 10, 2026
  9. Adrian RatiuFeb 10, 2026
  10. 1/4 hook: run a list of hooksAdrian Ratiu, Feb 4, 2026
  11. Junio C HamanoFeb 5, 2026
  12. Adrian RatiuFeb 6, 2026
  13. Patrick SteinhardtFeb 9, 2026
  14. Adrian RatiuFeb 9, 2026
  15. Patrick SteinhardtFeb 10, 2026
  16. 4/4 hook: allow out-of-repo 'git hook' invocationsAdrian Ratiu, Feb 4, 2026
  17. Junio C HamanoFeb 6, 2026
  18. 0/8 Specify hooks via configsAdrian Ratiu, Feb 18, 2026
  19. 2/8 hook: run a list of hooks to prepare for multihook supportAdrian Ratiu, Feb 18, 2026
  20. Patrick SteinhardtFeb 20, 2026
  21. Adrian RatiuFeb 20, 2026
  22. 1/8 hook: add internal state alloc/free callbacksAdrian Ratiu, Feb 18, 2026
  23. Junio C HamanoFeb 19, 2026
  24. Adrian RatiuFeb 20, 2026
  25. Junio C HamanoFeb 20, 2026
  26. Adrian RatiuFeb 20, 2026
  27. Patrick SteinhardtFeb 20, 2026
  28. Adrian RatiuFeb 20, 2026
  29. 3/8 hook: add "git hook list" commandAdrian Ratiu, Feb 18, 2026
  30. Patrick SteinhardtFeb 20, 2026
  31. Adrian RatiuFeb 20, 2026
  32. 5/8 hook: allow disabling config hooksAdrian Ratiu, Feb 18, 2026
  33. Patrick SteinhardtFeb 20, 2026
  34. Adrian RatiuFeb 20, 2026
  35. Patrick SteinhardtFeb 20, 2026
  36. Junio C HamanoFeb 20, 2026
  37. 6/8 hook: allow event = "" to overwrite previous valuesAdrian Ratiu, Feb 18, 2026
  38. 4/8 hook: include hooks from the configAdrian Ratiu, Feb 18, 2026
  39. Junio C HamanoFeb 19, 2026
  40. Adrian RatiuFeb 20, 2026
  41. Patrick SteinhardtFeb 20, 2026
  42. Adrian RatiuFeb 20, 2026
  43. 7/8 hook: allow out-of-repo 'git hook' invocationsAdrian Ratiu, Feb 18, 2026
  44. 8/8 hook: add -z option to "git hook list"Adrian Ratiu, Feb 18, 2026
  45. Junio C HamanoFeb 19, 2026
  46. Adrian RatiuFeb 20, 2026
  47. brian m. carlsonFeb 20, 2026
  48. Adrian RatiuFeb 21, 2026
  49. Adrian RatiuFeb 22, 2026
  50. Junio C HamanoFeb 25, 2026
  51. Adrian RatiuFeb 26, 2026
  52. brian m. carlsonFeb 25, 2026
  53. Adrian RatiuFeb 26, 2026
  54. 00/12 [next] Specify hooks via configsAdrian Ratiu, Mar 1, 2026
  55. 02/12 hook: run a list of hooks to prepare for multihook supportAdrian Ratiu, Mar 1, 2026
  56. 03/12 hook: add "git hook list" commandAdrian Ratiu, Mar 1, 2026
  57. 01/12 hook: add internal state alloc/free callbacksAdrian Ratiu, Mar 1, 2026
  58. 04/12 string-list: add unsorted_string_list_remove()Adrian Ratiu, Mar 1, 2026
  59. 06/12 hook: allow disabling config hooksAdrian Ratiu, Mar 1, 2026
  60. 05/12 hook: include hooks from the configAdrian Ratiu, Mar 1, 2026
  61. SZEDER GáborApr 6, 2026
  62. Adrian RatiuApr 8, 2026
  63. 07/12 hook: allow event = "" to overwrite previous valuesAdrian Ratiu, Mar 1, 2026
  64. 08/12 hook: allow out-of-repo 'git hook' invocationsAdrian Ratiu, Mar 1, 2026
  65. 09/12 hook: add -z option to "git hook list"Adrian Ratiu, Mar 1, 2026
  66. 10/12 hook: refactor hook_config_cache from strmap to named structAdrian Ratiu, Mar 1, 2026
  67. 11/12 hook: store and display scope for configured hooks in git hook listAdrian Ratiu, Mar 1, 2026
  68. 12/12 hook: show disabled hooks in "git hook list"Adrian Ratiu, Mar 1, 2026
  69. Junio C HamanoMar 2, 2026
  70. Adrian RatiuMar 2, 2026
  71. Junio C HamanoMar 2, 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.