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

[PATCH 8/8] hook-list.h: add a generated list of hooks, like config-list.h

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Sep 23, 2021, 10:30 UTC
Message-ID
<patch-8.8-80aae4d5c13-20210923T095326Z-avarab@gmail.com>
In-Reply-To
<cover-0.8-00000000000-20210923T095326Z-avarab@gmail.com>

Make githooks(5) the source of truth for what hooks git supports, and punt out early on hooks we don't know about in find_hook(). This ensures that the documentation and the C code's idea about existing hooks doesn't diverge.

We still have Perl and Python code running its own hooks, but that'll be addressed by Emily Shaffer's upcoming "git hook run" command.

This resolves a long-standing TODO item in bugreport.c of there being no centralized listing of hooks, and fixes a bug with the bugreport listing only knowing about 1/4 of the p4 hooks. It didn't know about the recent "reference-transaction" hook either.

We could make the find_hook() function die() or BUG() out if the new known_hook() returned 0, but let's make it return NULL just as it does when it can't find a hook of a known type. Making it die() is overly anal, and unlikely to be what we need in catching stupid typos in the name of some new hook hardcoded in git.git's sources. By making this be tolerant of unknown hook names, changes in a later series to make "git hook run" run arbitrary user-configured hook names will be easier to implement.

I have not been able to directly test the CMake change being made here. Since 4c2c38e800 (ci: modification of main.yml to use cmake for vs-build job, 2020-06-26) some of the Windows CI has a hard dependency on CMake, this change works there, and is to my eyes an obviously correct use of a pattern established in previous CMake changes, namely:

 - 061c2240b1 (Introduce CMake support for configuring Git,
    2020-06-12)
 - 709df95b78 (help: move list_config_help to builtin/help,
    2020-04-16)
 - 976aaedca0 (msvc: add a Makefile target to pre-generate the Visual
   Studio solution, 2019-07-29)

The LC_ALL=C is needed because at least in my locale the dash ("-") is ignored for the purposes of sorting, which results in a different order. I'm not aware of anything in git that has a hard dependency on the order, but e.g. the bugreport output would end up using whatever locale was in effect when git was compiled.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Helped-by: René Scharfe <l.s.r@web.de>
---
 .gitignore                          |  1 +
 Makefile                            |  9 +++++-
 builtin/bugreport.c                 | 44 ++++++-----------------------
 contrib/buildsystems/CMakeLists.txt |  7 +++++
 generate-hooklist.sh                | 18 ++++++++++++
 5 files changed, 42 insertions(+), 37 deletions(-)
 create mode 100755 generate-hooklist.sh
diff --git a/.gitignore b/.gitignore
index 311841f9bed..6be9de41ae8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -190,6 +190,7 @@
 /gitweb/static/gitweb.min.*
 /config-list.h
 /command-list.h
+/hook-list.h
 *.tar.gz
 *.dsc
 *.deb
diff --git a/Makefile b/Makefile
index ad2aeff68f0..f883657fa26 100644
--- a/Makefile
+++ b/Makefile
@@ -817,6 +817,8 @@ XDIFF_LIB = xdiff/lib.a
 
 GENERATED_H += command-list.h
 GENERATED_H += config-list.h
+GENERATED_H += hook-list.h
+
 .PHONY: generated-hdrs
 generated-hdrs: $(GENERATED_H)
 
@@ -2216,7 +2218,9 @@ git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS)
 
 help.sp help.s help.o: command-list.h
 
-builtin/help.sp builtin/help.s builtin/help.o: config-list.h GIT-PREFIX
+hook.sp hook.s hook.o: hook-list.h
+
+builtin/help.sp builtin/help.s builtin/help.o: config-list.h hook-list.h GIT-PREFIX
 builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \
 	'-DGIT_HTML_PATH="$(htmldir_relative_SQ)"' \
 	'-DGIT_MAN_PATH="$(mandir_relative_SQ)"' \
@@ -2248,6 +2252,9 @@ command-list.h: $(wildcard Documentation/git*.txt)
 		$(patsubst %,--exclude-program %,$(EXCLUDED_PROGRAMS)) \
 		command-list.txt >$@
 
+hook-list.h: generate-hooklist.sh Documentation/githooks.txt
+	$(QUIET_GEN)$(SHELL_PATH) ./generate-hooklist.sh >$@
+
 SCRIPT_DEFINES = $(SHELL_PATH_SQ):$(DIFF_SQ):$(GIT_VERSION):\
 	$(localedir_SQ):$(NO_CURL):$(USE_GETTEXT_SCHEME):$(SANE_TOOL_PATH_SQ):\
 	$(gitwebdir_SQ):$(PERL_PATH_SQ):$(SANE_TEXT_GREP):$(PAGER_ENV):\
diff --git a/builtin/bugreport.c b/builtin/bugreport.c
index a02c2540bb1..9de32bc96e7 100644
--- a/builtin/bugreport.c
+++ b/builtin/bugreport.c
@@ -4,6 +4,7 @@
 #include "help.h"
 #include "compat/compiler.h"
 #include "hook.h"
+#include "hook-list.h"
 
 
 static void get_system_info(struct strbuf *sys_info)
@@ -41,39 +42,7 @@ static void get_system_info(struct strbuf *sys_info)
 
 static void get_populated_hooks(struct strbuf *hook_info, int nongit)
 {
-	/*
-	 * NEEDSWORK: Doesn't look like there is a list of all possible hooks;
-	 * so below is a transcription of `git help hooks`. Later, this should
-	 * be replaced with some programmatically generated list (generated from
-	 * doc or else taken from some library which tells us about all the
-	 * hooks)
-	 */
-	static const char *hook[] = {
-		"applypatch-msg",
-		"pre-applypatch",
-		"post-applypatch",
-		"pre-commit",
-		"pre-merge-commit",
-		"prepare-commit-msg",
-		"commit-msg",
-		"post-commit",
-		"pre-rebase",
-		"post-checkout",
-		"post-merge",
-		"pre-push",
-		"pre-receive",
-		"update",
-		"post-receive",
-		"post-update",
-		"push-to-checkout",
-		"pre-auto-gc",
-		"post-rewrite",
-		"sendemail-validate",
-		"fsmonitor-watchman",
-		"p4-pre-submit",
-		"post-index-change",
-	};
-	int i;
+	const char **p;
 
 	if (nongit) {
 		strbuf_addstr(hook_info,
@@ -81,9 +50,12 @@ static void get_populated_hooks(struct strbuf *hook_info, int nongit)
 		return;
 	}
 
-	for (i = 0; i < ARRAY_SIZE(hook); i++)
-		if (hook_exists(hook[i]))
-			strbuf_addf(hook_info, "%s\n", hook[i]);
+	for (p = hook_name_list; *p; p++) {
+		const char *hook = *p;
+
+		if (hook_exists(hook))
+			strbuf_addf(hook_info, "%s\n", hook);
+	}
 }
 
 static const char * const bugreport_usage[] = {
diff --git a/contrib/buildsystems/CMakeLists.txt b/contrib/buildsystems/CMakeLists.txt
index 171b4124afe..fd1399c440f 100644
--- a/contrib/buildsystems/CMakeLists.txt
+++ b/contrib/buildsystems/CMakeLists.txt
@@ -624,6 +624,13 @@ if(NOT EXISTS ${CMAKE_BINARY_DIR}/config-list.h)
 			OUTPUT_FILE ${CMAKE_BINARY_DIR}/config-list.h)
 endif()
 
+if(NOT EXISTS ${CMAKE_BINARY_DIR}/hook-list.h)
+	message("Generating hook-list.h")
+	execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-hooklist.sh
+			WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+			OUTPUT_FILE ${CMAKE_BINARY_DIR}/hook-list.h)
+endif()
+
 include_directories(${CMAKE_BINARY_DIR})
 
 #build
diff --git a/generate-hooklist.sh b/generate-hooklist.sh
new file mode 100755
index 00000000000..6d4e56d1a31
--- /dev/null
+++ b/generate-hooklist.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+#
+# Usage: ./generate-hooklist.sh >hook-list.h
+
+cat <<EOF
+/* Automatically generated by generate-hooklist.sh */
+
+static const char *hook_name_list[] = {
+EOF
+
+sed -n -e '/^~~~~*$/ {x; s/^.*$/	"&",/; p;}; x' \
+	<Documentation/githooks.txt |
+	LC_ALL=C sort
+
+cat <<EOF
+	NULL,
+};
+EOF
-- 
2.33.0.1229.g0a86d28df49
Previous: Ævar Arnfjörð BjarmasonNext: Phillip Wood
Message 9 of 39 in “Makefile: generate a hook-list.h, prep for config-based-hooks”
  1. 0/8 Makefile: generate a hook-list.h, prep for config-based-hooksÆvar Arnfjörð Bjarmason, Sep 23, 2021
  2. 1/8 Makefile: mark "check" target as .PHONYÆvar Arnfjörð Bjarmason, Sep 23, 2021
  3. 2/8 Makefile: stop hardcoding {command,config}-list.hÆvar Arnfjörð Bjarmason, Sep 23, 2021
  4. 3/8 Makefile: don't perform "mv $@+ $@" dance for $(GENERATED_H)Ævar Arnfjörð Bjarmason, Sep 23, 2021
  5. 4/8 Makefile: remove an out-of-date commentÆvar Arnfjörð Bjarmason, Sep 23, 2021
  6. 5/8 hook.[ch]: move find_hook() from run-command.c to hook.cÆvar Arnfjörð Bjarmason, Sep 23, 2021
  7. 6/8 hook.c: add a hook_exists() wrapper and use it in bugreport.cÆvar Arnfjörð Bjarmason, Sep 23, 2021
  8. 7/8 hook.c users: use "hook_exists()" instead of "find_hook()"Ævar Arnfjörð Bjarmason, Sep 23, 2021
  9. 8/8 hook-list.h: add a generated list of hooks, like config-list.hÆvar Arnfjörð Bjarmason, Sep 23, 2021
  10. Phillip WoodSep 24, 2021
  11. Junio C HamanoSep 24, 2021
  12. René ScharfeSep 24, 2021
  13. Ævar Arnfjörð BjarmasonSep 24, 2021
  14. René ScharfeSep 24, 2021
  15. Ævar Arnfjörð BjarmasonSep 24, 2021
  16. Phillip WoodSep 27, 2021
  17. Ævar Arnfjörð BjarmasonSep 27, 2021
  18. Mike HommeyNov 15, 2021
  19. Ævar Arnfjörð BjarmasonNov 15, 2021
  20. Mike HommeyNov 15, 2021
  21. Ævar Arnfjörð BjarmasonNov 15, 2021
  22. Mike HommeyNov 15, 2021
  23. Ævar Arnfjörð BjarmasonNov 16, 2021
  24. Junio C HamanoNov 17, 2021
  25. 0/8 Makefile: generate a hook-list.h, prep for config-based-hooksÆvar Arnfjörð Bjarmason, Sep 26, 2021
  26. 1/8 Makefile: mark "check" target as .PHONYÆvar Arnfjörð Bjarmason, Sep 26, 2021
  27. 2/8 Makefile: stop hardcoding {command,config}-list.hÆvar Arnfjörð Bjarmason, Sep 26, 2021
  28. 3/8 Makefile: don't perform "mv $@+ $@" dance for $(GENERATED_H)Ævar Arnfjörð Bjarmason, Sep 26, 2021
  29. 4/8 Makefile: remove an out-of-date commentÆvar Arnfjörð Bjarmason, Sep 26, 2021
  30. 5/8 hook.[ch]: move find_hook() from run-command.c to hook.cÆvar Arnfjörð Bjarmason, Sep 26, 2021
  31. 6/8 hook.c: add a hook_exists() wrapper and use it in bugreport.cÆvar Arnfjörð Bjarmason, Sep 26, 2021
  32. 7/8 hook.c users: use "hook_exists()" instead of "find_hook()"Ævar Arnfjörð Bjarmason, Sep 26, 2021
  33. 8/8 hook-list.h: add a generated list of hooks, like config-list.hÆvar Arnfjörð Bjarmason, Sep 26, 2021
  34. Junio C HamanoSep 27, 2021
  35. René ScharfeSep 27, 2021
  36. Junio C HamanoSep 27, 2021
  37. Phillip WoodSep 27, 2021
  38. Ævar Arnfjörð BjarmasonSep 27, 2021
  39. Phillip WoodSep 27, 2021

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.