diff mbox series

[v4,3/5] hook: introduce "git hook list"

Message ID patch-v4-3.5-3e647b8dba7-20210909T122802Z-avarab@gmail.com (mailing list archive)
State New, archived
Headers show
Series config-based hooks restarted | expand

Commit Message

Ævar Arnfjörð Bjarmason Sept. 9, 2021, 12:42 p.m. UTC
From: Emily Shaffer <emilyshaffer@google.com>

If more than one hook will be run, it may be useful to see a list of
which hooks should be run. At very least, it will be useful for us to
test the semantics of multihooks ourselves.

For now, only list the hooks which will run in the order they will run
in; later, it might be useful to include more information like where the
hooks were configured and whether or not they will run.

Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 Documentation/git-hook.txt |  5 ++++
 builtin/hook.c             | 52 ++++++++++++++++++++++++++++++++++++++
 t/t1800-hook.sh            | 14 ++++++++++
 3 files changed, 71 insertions(+)
diff mbox series

Patch

diff --git a/Documentation/git-hook.txt b/Documentation/git-hook.txt
index 79e82479ec6..88588b38143 100644
--- a/Documentation/git-hook.txt
+++ b/Documentation/git-hook.txt
@@ -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
 -------
 
diff --git a/builtin/hook.c b/builtin/hook.c
index 9b6272cfd3b..1e6b15d565a 100644
--- a/builtin/hook.c
+++ b/builtin/hook.c
@@ -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);
 
diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh
index 6431b19e392..7a1dae4e95e 100755
--- a/t/t1800-hook.sh
+++ b/t/t1800-hook.sh
@@ -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