hook: add "git hook list" command

The previous commit introduced an ability to run multiple commands for hook events and next commit will introduce the ability to define hooks from configs, in addition to the "traditional" hooks from the hookdir. Introduce a new command "git hook list" to make inspecting hooks easier both for users and for the tests we will add. Further commits will expand on this, e.g. by adding a -z output mode. Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Emily Shaffer committed Feb 19, 2026 at 00:23 UTC 9fdaa6788924d4bb5ffc3a5908dae8a50e072f77
5 files changed +112 -16
Documentation/git-hook.adoc
+5
@@ -9,6 +9,7 @@ SYNOPSIS
9 --------
10 [verse]
11 'git hook' run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]
12 +'git hook' list <hook-name>
13
14 DESCRIPTION
15 -----------
@@ -28,6 +29,10 @@ Any positional arguments to the hook should be passed after a
29 mandatory `--` (or `--end-of-options`, see linkgit:gitcli[7]). See
30 linkgit:githooks[5] for arguments hooks might expect (if any).
31
32 +list::
33 + Print a list of hooks which will be run on `<hook-name>` event. If no
34 + hooks are configured for that event, print a warning and return 1.
35 +
36 OPTIONS
37 -------
38
builtin/hook.c
+60
@@ -6,12 +6,16 @@
6 #include "hook.h"
7 #include "parse-options.h"
8 #include "strvec.h"
9 +#include "abspath.h"
10
11 #define BUILTIN_HOOK_RUN_USAGE \
12 N_("git hook run [--ignore-missing] [--to-stdin=<path>] <hook-name> [-- <hook-args>]")
13 +#define BUILTIN_HOOK_LIST_USAGE \
14 + N_("git hook list <hook-name>")
15
16 static const char * const builtin_hook_usage[] = {
17 BUILTIN_HOOK_RUN_USAGE,
18 + BUILTIN_HOOK_LIST_USAGE,
19 NULL
20 };
21
@@ -20,6 +24,61 @@ static const char * const builtin_hook_run_usage[] = {
24 NULL
25 };
26
27 +static int list(int argc, const char **argv, const char *prefix,
28 + struct repository *repo)
29 +{
30 + static const char *const builtin_hook_list_usage[] = {
31 + BUILTIN_HOOK_LIST_USAGE,
32 + NULL
33 + };
34 + struct string_list *head;
35 + struct string_list_item *item;
36 + const char *hookname = NULL;
37 + int ret = 0;
38 +
39 + struct option list_options[] = {
40 + OPT_END(),
41 + };
42 +
43 + argc = parse_options(argc, argv, prefix, list_options,
44 + builtin_hook_list_usage, 0);
45 +
46 + /*
47 + * The only unnamed argument provided should be the hook-name; if we add
48 + * arguments later they probably should be caught by parse_options.
49 + */
50 + if (argc != 1)
51 + usage_msg_opt(_("You must specify a hook event name to list."),
52 + builtin_hook_list_usage, list_options);
53 +
54 + hookname = argv[0];
55 +
56 + head = list_hooks(repo, hookname, NULL);
57 +
58 + if (!head->nr) {
59 + warning(_("No hooks found for event '%s'"), hookname);
60 + ret = 1; /* no hooks found */
61 + goto cleanup;
62 + }
63 +
64 + for_each_string_list_item(item, head) {
65 + struct hook *h = item->util;
66 +
67 + switch (h->kind) {
68 + case HOOK_TRADITIONAL:
69 + printf("%s\n", _("hook from hookdir"));
70 + break;
71 + default:
72 + BUG("unknown hook kind");
73 + }
74 + }
75 +
76 +cleanup:
77 + hook_list_clear(head, NULL);
78 + free(head);
79 + return ret;
80 +}
81 +
82 static int run(int argc, const char **argv, const char *prefix,
83 struct repository *repo UNUSED)
84 {
@@ -77,6 +136,7 @@ int cmd_hook(int argc,
136 parse_opt_subcommand_fn *fn = NULL;
137 struct option builtin_hook_options[] = {
138 OPT_SUBCOMMAND("run", &fn, run),
139 + OPT_SUBCOMMAND("list", &fn, list),
140 OPT_END(),
141 };
142
hook.c
+2 -15
@@ -61,7 +61,7 @@ static void hook_clear(struct hook *h, cb_data_free_fn cb_data_free)
61 free(h);
62 }
63
64 -static void hook_list_clear(struct string_list *hooks, cb_data_free_fn cb_data_free)
64 +void hook_list_clear(struct string_list *hooks, cb_data_free_fn cb_data_free)
65 {
66 struct string_list_item *item;
67
@@ -101,20 +101,7 @@ static void list_hooks_add_default(struct repository *r, const char *hookname,
101 string_list_append(hook_list, hook_path)->util = h;
102 }
103
104 -/*
105 - * Provides a list of hook commands to run for the 'hookname' event.
106 - *
107 - * This function consolidates hooks from two sources:
108 - * 1. The config-based hooks (not yet implemented).
109 - * 2. The "traditional" hook found in the repository hooks directory
110 - * (e.g., .git/hooks/pre-commit).
111 - *
112 - * The list is ordered by execution priority.
113 - *
114 - * The caller is responsible for freeing the memory of the returned list
115 - * using string_list_clear() and free().
116 - */
117 -static struct string_list *list_hooks(struct repository *r, const char *hookname,
104 +struct string_list *list_hooks(struct repository *r, const char *hookname,
105 struct run_hooks_opt *options)
106 {
107 struct string_list *hook_head;
hook.h
+23 -1
@@ -163,7 +163,29 @@ struct hook_cb_data {
163 struct run_hooks_opt *options;
164 };
165
166 -/*
166 +/**
167 + * Provides a list of hook commands to run for the 'hookname' event.
168 + *
169 + * This function consolidates hooks from two sources:
170 + * 1. The config-based hooks (not yet implemented).
171 + * 2. The "traditional" hook found in the repository hooks directory
172 + * (e.g., .git/hooks/pre-commit).
173 + *
174 + * The list is ordered by execution priority.
175 + *
176 + * The caller is responsible for freeing the memory of the returned list
177 + * using string_list_clear() and free().
178 + */
179 +struct string_list *list_hooks(struct repository *r, const char *hookname,
180 + struct run_hooks_opt *options);
181 +
182 +/**
183 + * Frees the memory allocated for the hook list, including the `struct hook`
184 + * items and their internal state.
185 + */
186 +void hook_list_clear(struct string_list *hooks, cb_data_free_fn cb_data_free);
187 +
188 +/**
189 * Returns the path to the hook file, or NULL if the hook is missing
190 * or disabled. Note that this points to static storage that will be
191 * overwritten by further calls to find_hook and run_hook_*.
t/t1800-hook.sh
+22
@@ -10,9 +10,31 @@ test_expect_success 'git hook usage' '
10 test_expect_code 129 git hook run &&
11 test_expect_code 129 git hook run -h &&
12 test_expect_code 129 git hook run --unknown 2>err &&
13 + test_expect_code 129 git hook list &&
14 + test_expect_code 129 git hook list -h &&
15 grep "unknown option" err
16 '
17
18 +test_expect_success 'git hook list: nonexistent hook' '
19 + cat >stderr.expect <<-\EOF &&
20 + warning: No hooks found for event '\''test-hook'\''
21 + EOF
22 + test_expect_code 1 git hook list test-hook 2>stderr.actual &&
23 + test_cmp stderr.expect stderr.actual
24 +'
25 +
26 +test_expect_success 'git hook list: traditional hook from hookdir' '
27 + test_hook test-hook <<-EOF &&
28 + echo Test hook
29 + EOF
30 +
31 + cat >expect <<-\EOF &&
32 + hook from hookdir
33 + EOF
34 + git hook list test-hook >actual &&
35 + test_cmp expect actual
36 +'
37 +
38 test_expect_success 'git hook run: nonexistent hook' '
39 cat >stderr.expect <<-\EOF &&
40 error: cannot find a hook named test-hook