Skip to content

Commit a5e61a4

Browse files
committed
Merge branch 'ab/config-based-hooks-1'
Mostly preliminary clean-up in the hook API. * ab/config-based-hooks-1: hook-list.h: add a generated list of hooks, like config-list.h hook.c users: use "hook_exists()" instead of "find_hook()" hook.c: add a hook_exists() wrapper and use it in bugreport.c hook.[ch]: move find_hook() from run-command.c to hook.c Makefile: remove an out-of-date comment Makefile: don't perform "mv $@+ $@" dance for $(GENERATED_H) Makefile: stop hardcoding {command,config}-list.h Makefile: mark "check" target as .PHONY
2 parents cf00603 + cfe853e commit a5e61a4

File tree

19 files changed

+127
-100
lines changed

19 files changed

+127
-100
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: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,10 @@ 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+
822+
.PHONY: generated-hdrs
823+
generated-hdrs: $(GENERATED_H)
820824

821825
LIB_H := $(sort $(patsubst ./%,%,$(shell git ls-files '*.h' ':!t/' ':!Documentation/' 2>/dev/null || \
822826
$(FIND) . \
@@ -902,6 +906,7 @@ LIB_OBJS += hash-lookup.o
902906
LIB_OBJS += hashmap.o
903907
LIB_OBJS += help.o
904908
LIB_OBJS += hex.o
909+
LIB_OBJS += hook.o
905910
LIB_OBJS += ident.o
906911
LIB_OBJS += json-writer.o
907912
LIB_OBJS += kwset.o
@@ -2211,8 +2216,9 @@ git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS)
22112216
$(filter %.o,$^) $(LIBS)
22122217

22132218
help.sp help.s help.o: command-list.h
2219+
hook.sp hook.s hook.o: hook-list.h
22142220

2215-
builtin/help.sp builtin/help.s builtin/help.o: config-list.h GIT-PREFIX
2221+
builtin/help.sp builtin/help.s builtin/help.o: config-list.h hook-list.h GIT-PREFIX
22162222
builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \
22172223
'-DGIT_HTML_PATH="$(htmldir_relative_SQ)"' \
22182224
'-DGIT_MAN_PATH="$(mandir_relative_SQ)"' \
@@ -2235,15 +2241,17 @@ $(BUILT_INS): git$X
22352241
config-list.h: generate-configlist.sh
22362242

22372243
config-list.h: Documentation/*config.txt Documentation/config/*.txt
2238-
$(QUIET_GEN)$(SHELL_PATH) ./generate-configlist.sh \
2239-
>$@+ && mv $@+ $@
2244+
$(QUIET_GEN)$(SHELL_PATH) ./generate-configlist.sh >$@
22402245

22412246
command-list.h: generate-cmdlist.sh command-list.txt
22422247

22432248
command-list.h: $(wildcard Documentation/git*.txt)
22442249
$(QUIET_GEN)$(SHELL_PATH) ./generate-cmdlist.sh \
22452250
$(patsubst %,--exclude-program %,$(EXCLUDED_PROGRAMS)) \
2246-
command-list.txt >$@+ && mv $@+ $@
2251+
command-list.txt >$@
2252+
2253+
hook-list.h: generate-hooklist.sh Documentation/githooks.txt
2254+
$(QUIET_GEN)$(SHELL_PATH) ./generate-hooklist.sh >$@
22472255

22482256
SCRIPT_DEFINES = $(SHELL_PATH_SQ):$(DIFF_SQ):$(GIT_VERSION):\
22492257
$(localedir_SQ):$(NO_CURL):$(USE_GETTEXT_SCHEME):$(SANE_TOOL_PATH_SQ):\
@@ -2505,13 +2513,6 @@ ifneq ($(dep_files_present),)
25052513
include $(dep_files_present)
25062514
endif
25072515
else
2508-
# Dependencies on header files, for platforms that do not support
2509-
# the gcc -MMD option.
2510-
#
2511-
# Dependencies on automatically generated headers such as command-list.h
2512-
# should _not_ be included here, since they are necessary even when
2513-
# building an object for the first time.
2514-
25152516
$(OBJECTS): $(LIB_H) $(GENERATED_H)
25162517
endif
25172518

@@ -2909,7 +2910,7 @@ $(SP_OBJ): %.sp: %.c GIT-CFLAGS FORCE
29092910
.PHONY: sparse $(SP_OBJ)
29102911
sparse: $(SP_OBJ)
29112912

2912-
EXCEPT_HDRS := command-list.h config-list.h unicode-width.h compat/% xdiff/%
2913+
EXCEPT_HDRS := $(GENERATED_H) unicode-width.h compat/% xdiff/%
29132914
ifndef GCRYPT_SHA256
29142915
EXCEPT_HDRS += sha256/gcrypt.h
29152916
endif
@@ -2931,7 +2932,8 @@ hdr-check: $(HCO)
29312932
style:
29322933
git clang-format --style file --diff --extensions c,h
29332934

2934-
check: config-list.h command-list.h
2935+
.PHONY: check
2936+
check: $(GENERATED_H)
29352937
@if sparse; \
29362938
then \
29372939
echo >&2 "Use 'make sparse' instead"; \

builtin/am.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "parse-options.h"
1212
#include "dir.h"
1313
#include "run-command.h"
14+
#include "hook.h"
1415
#include "quote.h"
1516
#include "tempfile.h"
1617
#include "lockfile.h"

builtin/bugreport.c

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include "strbuf.h"
44
#include "help.h"
55
#include "compat/compiler.h"
6-
#include "run-command.h"
6+
#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 (find_hook(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[] = {

builtin/commit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "revision.h"
2020
#include "wt-status.h"
2121
#include "run-command.h"
22+
#include "hook.h"
2223
#include "refs.h"
2324
#include "log-tree.h"
2425
#include "strbuf.h"
@@ -1051,7 +1052,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
10511052
return 0;
10521053
}
10531054

1054-
if (!no_verify && find_hook("pre-commit")) {
1055+
if (!no_verify && hook_exists("pre-commit")) {
10551056
/*
10561057
* Re-read the index as pre-commit hook could have updated it,
10571058
* and write it out as a tree. We must do this before we invoke

builtin/merge.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "builtin.h"
1414
#include "lockfile.h"
1515
#include "run-command.h"
16+
#include "hook.h"
1617
#include "diff.h"
1718
#include "diff-merges.h"
1819
#include "refs.h"
@@ -849,7 +850,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
849850
* and write it out as a tree. We must do this before we invoke
850851
* the editor and after we invoke run_status above.
851852
*/
852-
if (find_hook("pre-merge-commit"))
853+
if (hook_exists("pre-merge-commit"))
853854
discard_cache();
854855
read_cache_from(index_file);
855856
strbuf_addbuf(&msg, &merge_msg);

builtin/receive-pack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "pkt-line.h"
88
#include "sideband.h"
99
#include "run-command.h"
10+
#include "hook.h"
1011
#include "exec-cmd.h"
1112
#include "commit.h"
1213
#include "object.h"
@@ -1463,7 +1464,7 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *w
14631464

14641465
strvec_pushf(&env, "GIT_DIR=%s", absolute_path(git_dir));
14651466

1466-
if (!find_hook(push_to_checkout_hook))
1467+
if (!hook_exists(push_to_checkout_hook))
14671468
retval = push_to_deploy(sha1, &env, work_tree);
14681469
else
14691470
retval = push_to_checkout(sha1, &env, work_tree);

builtin/worktree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "branch.h"
99
#include "refs.h"
1010
#include "run-command.h"
11+
#include "hook.h"
1112
#include "sigchain.h"
1213
#include "submodule.h"
1314
#include "utf8.h"

compat/vcbuild/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ The Steps of Build Git with VS2008
9292
the git operations.
9393

9494
3. Inside Git's directory run the command:
95-
make command-list.h config-list.h
95+
make generated-hdrs
9696
to generate the header file needed to compile git.
9797

9898
4. Then either build Git with the GNU Make Makefile in the Git projects

config.mak.uname

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,9 @@ vcxproj:
739739
echo '</Project>') >git-remote-http/LinkOrCopyRemoteHttp.targets
740740
git add -f git/LinkOrCopyBuiltins.targets git-remote-http/LinkOrCopyRemoteHttp.targets
741741

742-
# Add command-list.h and config-list.h
743-
$(MAKE) MSVC=1 SKIP_VCPKG=1 prefix=/mingw64 config-list.h command-list.h
744-
git add -f config-list.h command-list.h
742+
# Add generated headers
743+
$(MAKE) MSVC=1 SKIP_VCPKG=1 prefix=/mingw64 $(GENERATED_H)
744+
git add -f $(GENERATED_H)
745745

746746
# Add scripts
747747
rm -f perl/perl.mak

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

hook.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "cache.h"
2+
#include "hook.h"
3+
#include "run-command.h"
4+
5+
const char *find_hook(const char *name)
6+
{
7+
static struct strbuf path = STRBUF_INIT;
8+
9+
strbuf_reset(&path);
10+
strbuf_git_path(&path, "hooks/%s", name);
11+
if (access(path.buf, X_OK) < 0) {
12+
int err = errno;
13+
14+
#ifdef STRIP_EXTENSION
15+
strbuf_addstr(&path, STRIP_EXTENSION);
16+
if (access(path.buf, X_OK) >= 0)
17+
return path.buf;
18+
if (errno == EACCES)
19+
err = errno;
20+
#endif
21+
22+
if (err == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) {
23+
static struct string_list advise_given = STRING_LIST_INIT_DUP;
24+
25+
if (!string_list_lookup(&advise_given, name)) {
26+
string_list_insert(&advise_given, name);
27+
advise(_("The '%s' hook was ignored because "
28+
"it's not set as executable.\n"
29+
"You can disable this warning with "
30+
"`git config advice.ignoredHook false`."),
31+
path.buf);
32+
}
33+
}
34+
return NULL;
35+
}
36+
return path.buf;
37+
}
38+
39+
int hook_exists(const char *name)
40+
{
41+
return !!find_hook(name);
42+
}

hook.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef HOOK_H
2+
#define HOOK_H
3+
4+
/*
5+
* Returns the path to the hook file, or NULL if the hook is missing
6+
* or disabled. Note that this points to static storage that will be
7+
* overwritten by further calls to find_hook and run_hook_*.
8+
*/
9+
const char *find_hook(const char *name);
10+
11+
/**
12+
* A boolean version of find_hook()
13+
*/
14+
int hook_exists(const char *hookname);
15+
16+
#endif

refs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "refs.h"
1111
#include "refs/refs-internal.h"
1212
#include "run-command.h"
13+
#include "hook.h"
1314
#include "object-store.h"
1415
#include "object.h"
1516
#include "tag.h"

run-command.c

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "quote.h"
1010
#include "config.h"
1111
#include "packfile.h"
12+
#include "hook.h"
1213

1314
void child_process_init(struct child_process *child)
1415
{
@@ -1322,40 +1323,6 @@ int async_with_fork(void)
13221323
#endif
13231324
}
13241325

1325-
const char *find_hook(const char *name)
1326-
{
1327-
static struct strbuf path = STRBUF_INIT;
1328-
1329-
strbuf_reset(&path);
1330-
strbuf_git_path(&path, "hooks/%s", name);
1331-
if (access(path.buf, X_OK) < 0) {
1332-
int err = errno;
1333-
1334-
#ifdef STRIP_EXTENSION
1335-
strbuf_addstr(&path, STRIP_EXTENSION);
1336-
if (access(path.buf, X_OK) >= 0)
1337-
return path.buf;
1338-
if (errno == EACCES)
1339-
err = errno;
1340-
#endif
1341-
1342-
if (err == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) {
1343-
static struct string_list advise_given = STRING_LIST_INIT_DUP;
1344-
1345-
if (!string_list_lookup(&advise_given, name)) {
1346-
string_list_insert(&advise_given, name);
1347-
advise(_("The '%s' hook was ignored because "
1348-
"it's not set as executable.\n"
1349-
"You can disable this warning with "
1350-
"`git config advice.ignoredHook false`."),
1351-
path.buf);
1352-
}
1353-
}
1354-
return NULL;
1355-
}
1356-
return path.buf;
1357-
}
1358-
13591326
int run_hook_ve(const char *const *env, const char *name, va_list args)
13601327
{
13611328
struct child_process hook = CHILD_PROCESS_INIT;

0 commit comments

Comments
 (0)