]> git.ipfire.org Git - thirdparty/git.git/commitdiff
hooks: allow customizing where the hook directory is
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Wed, 4 May 2016 22:58:12 +0000 (22:58 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 4 May 2016 23:25:13 +0000 (16:25 -0700)
Change the hardcoded lookup for .git/hooks/* to optionally lookup in
$(git config core.hooksPath)/* instead.

This is essentially a more intrusive version of the git-init ability to
specify hooks on init time via init templates.

The difference between that facility and this feature is that this can
be set up after the fact via e.g. ~/.gitconfig or /etc/gitconfig to
apply for all your personal repositories, or all repositories on the
system.

I plan on using this on a centralized Git server where users can create
arbitrary repositories under /gitroot, but I'd like to manage all the
hooks that should be run centrally via a unified dispatch mechanism.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config.txt
Documentation/githooks.txt
cache.h
config.c
environment.c
run-command.c
t/t1350-config-hooks-path.sh [new file with mode: 0755]

index 2cd6bdd7d2bc2816c1a9aed1c6a26bbd3285777c..7f8c119b2fe329227268ab841690268cad8bfa8e 100644 (file)
@@ -618,6 +618,23 @@ core.attributesFile::
        $XDG_CONFIG_HOME/git/attributes. If $XDG_CONFIG_HOME is either not
        set or empty, $HOME/.config/git/attributes is used instead.
 
+core.hooksPath::
+       By default Git will look for your hooks in the
+       '$GIT_DIR/hooks' directory. Set this to different path,
+       e.g. '/etc/git/hooks', and Git will try to find your hooks in
+       that directory, e.g. '/etc/git/hooks/pre-receive' instead of
+       in '$GIT_DIR/hooks/pre-receive'.
++
+The path can be either absolute or relative. A relative path is
+taken as relative to the directory where the hooks are run (see
+the "DESCRIPTION" section of linkgit:githooks[5]).
++
+This configuration variable is useful in cases where you'd like to
+centrally configure your Git hooks instead of configuring them on a
+per-repository basis, or as a more flexible and centralized
+alternative to having an `init.templateDir` where you've changed
+default hooks.
+
 core.editor::
        Commands such as `commit` and `tag` that lets you edit
        messages by launching an editor uses the value of this
index 30495745a0fa742e9b3c42dd2d110a71d97b97a1..d82e912e550ee0032c20a46c4238f7ce10003657 100644 (file)
@@ -7,15 +7,19 @@ githooks - Hooks used by Git
 
 SYNOPSIS
 --------
-$GIT_DIR/hooks/*
+$GIT_DIR/hooks/* (or \`git config core.hooksPath`/*)
 
 
 DESCRIPTION
 -----------
 
-Hooks are programs you can place in the `$GIT_DIR/hooks` directory to
-trigger actions at certain points in git's execution. Hooks that don't
-have the executable bit set are ignored.
+Hooks are programs you can place in a hooks directory to trigger
+actions at certain points in git's execution. Hooks that don't have
+the executable bit set are ignored.
+
+By default the hooks directory is `$GIT_DIR/hooks`, but that can be
+changed via the `core.hooksPath` configuration variable (see
+linkgit:git-config[1]).
 
 Before Git invokes a hook, it changes its working directory to either
 the root of the working tree in a non-bare repository, or to the
diff --git a/cache.h b/cache.h
index b829410f6da0afc14353b4621d2fdf874181a9f7..a7529b0233f5ce0647999e106ea9dbc58396039d 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -655,6 +655,7 @@ extern int shared_repository;
 extern const char *apply_default_whitespace;
 extern const char *apply_default_ignorewhitespace;
 extern const char *git_attributes_file;
+extern const char *git_hooks_path;
 extern int zlib_compression_level;
 extern int core_compression_level;
 extern int core_compression_seen;
index 9ba40bc1b039b9b65425dc4fa1bd9c7f1fcb0868..9ef5fde785317c6292a5bb1b23f6b8dc819aafb1 100644 (file)
--- a/config.c
+++ b/config.c
@@ -717,6 +717,9 @@ static int git_default_core_config(const char *var, const char *value)
        if (!strcmp(var, "core.attributesfile"))
                return git_config_pathname(&git_attributes_file, var, value);
 
+       if (!strcmp(var, "core.hookspath"))
+               return git_config_pathname(&git_hooks_path, var, value);
+
        if (!strcmp(var, "core.bare")) {
                is_bare_repository_cfg = git_config_bool(var, value);
                return 0;
index 6dec9d0403f11579a7ab316db87a1dbbc58e71b2..a14d1150238255e1de0bc4fe287627b91d0de620 100644 (file)
@@ -33,6 +33,7 @@ int shared_repository = PERM_UMASK;
 const char *apply_default_whitespace;
 const char *apply_default_ignorewhitespace;
 const char *git_attributes_file;
+const char *git_hooks_path;
 int zlib_compression_level = Z_BEST_SPEED;
 int core_compression_level;
 int core_compression_seen;
index c72601056cf5ae7be2593ae89af4effc26a1b043..aa85cd5c3ccd343df4727217089ec3c6055735cc 100644 (file)
@@ -815,7 +815,10 @@ const char *find_hook(const char *name)
        static struct strbuf path = STRBUF_INIT;
 
        strbuf_reset(&path);
-       strbuf_git_path(&path, "hooks/%s", name);
+       if (git_hooks_path)
+               strbuf_addf(&path, "%s/%s", git_hooks_path, name);
+       else
+               strbuf_git_path(&path, "hooks/%s", name);
        if (access(path.buf, X_OK) < 0)
                return NULL;
        return path.buf;
diff --git a/t/t1350-config-hooks-path.sh b/t/t1350-config-hooks-path.sh
new file mode 100755 (executable)
index 0000000..5e3fb3a
--- /dev/null
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+test_description='Test the core.hooksPath configuration variable'
+
+. ./test-lib.sh
+
+test_expect_success 'set up a pre-commit hook in core.hooksPath' '
+       mkdir -p .git/custom-hooks .git/hooks &&
+       write_script .git/custom-hooks/pre-commit <<-\EOF &&
+       echo CUSTOM >>actual
+       EOF
+       write_script .git/hooks/pre-commit <<-\EOF
+       echo NORMAL >>actual
+       EOF
+'
+
+test_expect_success 'Check that various forms of specifying core.hooksPath work' '
+       test_commit no_custom_hook &&
+       git config core.hooksPath .git/custom-hooks &&
+       test_commit have_custom_hook &&
+       git config core.hooksPath .git/custom-hooks/ &&
+       test_commit have_custom_hook_trailing_slash &&
+       git config core.hooksPath "$PWD/.git/custom-hooks" &&
+       test_commit have_custom_hook_abs_path &&
+       git config core.hooksPath "$PWD/.git/custom-hooks/" &&
+       test_commit have_custom_hook_abs_path_trailing_slash &&
+       cat >expect <<-\EOF &&
+       NORMAL
+       CUSTOM
+       CUSTOM
+       CUSTOM
+       CUSTOM
+       EOF
+       test_cmp expect actual
+'
+
+test_done