@@ -10,6 +10,7 @@ SYNOPSIS
[verse]
'git hook' run [--to-stdin=<path>] [--ignore-missing] [(-j|--jobs) <n>]
<hook-name> [-- <hook-args>]
+'git hook' list <hook-name>
DESCRIPTION
-----------
@@ -30,6 +31,10 @@ optional `--` (or `--end-of-options`, see linkgit:gitcli[7]). The
arguments (if any) differ by hook name, see linkgit:githooks[5] for
what those are.
+list::
+ Print a list of hooks which will be run on `<hook-name>` event. If no
+ hooks are configured for that event, print nothing and return 1.
+
OPTIONS
-------
@@ -8,8 +8,11 @@
#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 <hook-name>")
static const char * const builtin_hook_usage[] = {
+ BUILTIN_HOOK_LIST_USAGE,
BUILTIN_HOOK_RUN_USAGE,
NULL
};
@@ -19,6 +22,53 @@ static const char * const builtin_hook_run_usage[] = {
NULL
};
+static const char *const builtin_hook_list_usage[] = {
+ BUILTIN_HOOK_LIST_USAGE,
+ NULL
+};
+
+static int list(int argc, const char **argv, const char *prefix)
+{
+ struct list_head *hooks;
+ struct list_head *pos;
+ const char *hookname = NULL;
+ struct option list_options[] = {
+ OPT_END(),
+ };
+ int ret = 0;
+
+ argc = parse_options(argc, argv, prefix, list_options,
+ builtin_hook_list_usage, 0);
+
+ /*
+ * The only unnamed argument provided should be the hook-name; if we add
+ * arguments later they probably should be caught by parse_options.
+ */
+ if (argc != 1)
+ usage_msg_opt(_("You must specify a hook event name to list."),
+ builtin_hook_list_usage, list_options);
+
+ hookname = argv[0];
+
+ hooks = list_hooks(hookname);
+
+ if (list_empty(hooks)) {
+ ret = 1;
+ goto cleanup;
+ }
+
+ list_for_each(pos, hooks) {
+ struct hook *item = list_entry(pos, struct hook, list);
+ item = list_entry(pos, struct hook, list);
+ if (item)
+ printf("%s\n", item->hook_path);
+ }
+
+cleanup:
+ clear_hook_list(hooks);
+
+ return ret;
+}
static int run(int argc, const char **argv, const char *prefix)
{
int i;
@@ -94,6 +144,8 @@ int cmd_hook(int argc, const char **argv, const char *prefix)
if (!argc)
goto usage;
+ if (!strcmp(argv[0], "list"))
+ return list(argc, argv, prefix);
if (!strcmp(argv[0], "run"))
return run(argc, argv, prefix);
@@ -9,6 +9,8 @@ test_expect_success 'git hook usage' '
test_expect_code 129 git hook run &&
test_expect_code 129 git hook run -h &&
test_expect_code 129 git hook run --unknown 2>err &&
+ test_expect_code 129 git hook list &&
+ test_expect_code 129 git hook list -h &&
grep "unknown option" err
'
@@ -97,6 +99,18 @@ test_expect_success 'git hook run -- pass arguments' '
test_cmp expect actual
'
+test_expect_success 'git hook list: does-not-exist hook' '
+ test_expect_code 1 git hook list does-not-exist
+'
+
+test_expect_success 'git hook list: existing hook' '
+ cat >expect <<-\EOF &&
+ .git/hooks/test-hook
+ EOF
+ git hook list test-hook >actual &&
+ 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