Skip to content

Commit cfe853e

Browse files
avargitster
authored andcommitted
hook-list.h: add a generated list of hooks, like config-list.h
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 4c2c38e (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: - 061c224 (Introduce CMake support for configuring Git, 2020-06-12) - 709df95 (help: move list_config_help to builtin/help, 2020-04-16) - 976aaed (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 <[email protected]> Helped-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 07a348e commit cfe853e

File tree

5 files changed

+43
-37
lines changed

5 files changed

+43
-37
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
/gitweb/static/gitweb.min.*
191191
/config-list.h
192192
/command-list.h
193+
/hook-list.h
193194
*.tar.gz
194195
*.dsc
195196
*.deb

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,8 @@ XDIFF_LIB = xdiff/lib.a
817817

818818
GENERATED_H += command-list.h
819819
GENERATED_H += config-list.h
820+
GENERATED_H += hook-list.h
821+
820822
.PHONY: generated-hdrs
821823
generated-hdrs: $(GENERATED_H)
822824

@@ -2208,8 +2210,9 @@ git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS)
22082210
$(filter %.o,$^) $(LIBS)
22092211

22102212
help.sp help.s help.o: command-list.h
2213+
hook.sp hook.s hook.o: hook-list.h
22112214

2212-
builtin/help.sp builtin/help.s builtin/help.o: config-list.h GIT-PREFIX
2215+
builtin/help.sp builtin/help.s builtin/help.o: config-list.h hook-list.h GIT-PREFIX
22132216
builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \
22142217
'-DGIT_HTML_PATH="$(htmldir_relative_SQ)"' \
22152218
'-DGIT_MAN_PATH="$(mandir_relative_SQ)"' \
@@ -2241,6 +2244,9 @@ command-list.h: $(wildcard Documentation/git*.txt)
22412244
$(patsubst %,--exclude-program %,$(EXCLUDED_PROGRAMS)) \
22422245
command-list.txt >$@
22432246

2247+
hook-list.h: generate-hooklist.sh Documentation/githooks.txt
2248+
$(QUIET_GEN)$(SHELL_PATH) ./generate-hooklist.sh >$@
2249+
22442250
SCRIPT_DEFINES = $(SHELL_PATH_SQ):$(DIFF_SQ):$(GIT_VERSION):\
22452251
$(localedir_SQ):$(NO_CURL):$(USE_GETTEXT_SCHEME):$(SANE_TOOL_PATH_SQ):\
22462252
$(gitwebdir_SQ):$(PERL_PATH_SQ):$(SANE_TEXT_GREP):$(PAGER_ENV):\

builtin/bugreport.c

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "help.h"
55
#include "compat/compiler.h"
66
#include "hook.h"
7+
#include "hook-list.h"
78

89

910
static void get_system_info(struct strbuf *sys_info)
@@ -41,49 +42,20 @@ static void get_system_info(struct strbuf *sys_info)
4142

4243
static void get_populated_hooks(struct strbuf *hook_info, int nongit)
4344
{
44-
/*
45-
* NEEDSWORK: Doesn't look like there is a list of all possible hooks;
46-
* so below is a transcription of `git help hooks`. Later, this should
47-
* be replaced with some programmatically generated list (generated from
48-
* doc or else taken from some library which tells us about all the
49-
* hooks)
50-
*/
51-
static const char *hook[] = {
52-
"applypatch-msg",
53-
"pre-applypatch",
54-
"post-applypatch",
55-
"pre-commit",
56-
"pre-merge-commit",
57-
"prepare-commit-msg",
58-
"commit-msg",
59-
"post-commit",
60-
"pre-rebase",
61-
"post-checkout",
62-
"post-merge",
63-
"pre-push",
64-
"pre-receive",
65-
"update",
66-
"post-receive",
67-
"post-update",
68-
"push-to-checkout",
69-
"pre-auto-gc",
70-
"post-rewrite",
71-
"sendemail-validate",
72-
"fsmonitor-watchman",
73-
"p4-pre-submit",
74-
"post-index-change",
75-
};
76-
int i;
45+
const char **p;
7746

7847
if (nongit) {
7948
strbuf_addstr(hook_info,
8049
_("not run from a git repository - no hooks to show\n"));
8150
return;
8251
}
8352

84-
for (i = 0; i < ARRAY_SIZE(hook); i++)
85-
if (hook_exists(hook[i]))
86-
strbuf_addf(hook_info, "%s\n", hook[i]);
53+
for (p = hook_name_list; *p; p++) {
54+
const char *hook = *p;
55+
56+
if (hook_exists(hook))
57+
strbuf_addf(hook_info, "%s\n", hook);
58+
}
8759
}
8860

8961
static const char * const bugreport_usage[] = {

contrib/buildsystems/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,13 @@ if(NOT EXISTS ${CMAKE_BINARY_DIR}/config-list.h)
624624
OUTPUT_FILE ${CMAKE_BINARY_DIR}/config-list.h)
625625
endif()
626626

627+
if(NOT EXISTS ${CMAKE_BINARY_DIR}/hook-list.h)
628+
message("Generating hook-list.h")
629+
execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/generate-hooklist.sh
630+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
631+
OUTPUT_FILE ${CMAKE_BINARY_DIR}/hook-list.h)
632+
endif()
633+
627634
include_directories(${CMAKE_BINARY_DIR})
628635

629636
#build

generate-hooklist.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
#
3+
# Usage: ./generate-hooklist.sh >hook-list.h
4+
5+
cat <<EOF
6+
/* Automatically generated by generate-hooklist.sh */
7+
8+
static const char *hook_name_list[] = {
9+
EOF
10+
11+
sed -n \
12+
-e '/^~~~~*$/ {x; s/^.*$/ "&",/; p;}' \
13+
-e 'x' \
14+
<Documentation/githooks.txt |
15+
LC_ALL=C sort
16+
17+
cat <<EOF
18+
NULL,
19+
};
20+
EOF

0 commit comments

Comments
 (0)