git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH] hooks: Add ability to specify where the hook directory is
Date: Fri, 22 Apr 2016 23:33:17 +0000	[thread overview]
Message-ID: <1461367997-28745-1-git-send-email-avarab@gmail.com> (raw)

Change the hardcoded lookup for .git/hooks/* to optionally lookup in
$(git config core.hooksDirectory)/* instead if that config key is set.

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>
---
 Documentation/config.txt          | 10 ++++++++++
 Documentation/githooks.txt        |  5 ++++-
 cache.h                           |  1 +
 config.c                          |  3 +++
 environment.c                     |  1 +
 run-command.c                     |  5 ++++-
 t/t1350-config-hooks-directory.sh | 35 +++++++++++++++++++++++++++++++++++
 7 files changed, 58 insertions(+), 2 deletions(-)
 create mode 100755 t/t1350-config-hooks-directory.sh

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 42d2b50..2faf3c0 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -618,6 +618,16 @@ 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.hooksDirectory::
+	By default Git will look for your hooks in the '$GIT_DIR/hooks'
+	directory. Set this to different absolute directory name,
+	e.g. '/etc/git/hooks', and Git will try to find your hooks that
+	directory, e.g. '/etc/git/hooks/pre-receive' instead of in
+	'$GIT_DIR/hooks'.
++
+This is useful in cases where you'd like to centrally configure your
+Git hooks instead of configuring them on a per-repository basis.
+
 core.editor::
 	Commands such as `commit` and `tag` that lets you edit
 	messages by launching an editor uses the value of this
diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt
index a2f59b1..e1fe66d 100644
--- a/Documentation/githooks.txt
+++ b/Documentation/githooks.txt
@@ -13,13 +13,16 @@ $GIT_DIR/hooks/*
 DESCRIPTION
 -----------
 
-Hooks are little scripts you can place in `$GIT_DIR/hooks`
+Hooks are little scripts you can place in the `hooks`
 directory to trigger action at certain points.  When
 'git init' is run, a handful of example hooks are copied into the
 `hooks` directory of the new repository, but by default they are
 all disabled.  To enable a hook, rename it by removing its `.sample`
 suffix.
 
+By default the hooks directory is `$GIT_DIR/hooks`, but that can be
+changed via the `core.hooksDirectory` (see linkgit:git-config[1]).
+
 NOTE: It is also a requirement for a given hook to be executable.
 However - in a freshly initialized repository - the `.sample` files are
 executable by default.
diff --git a/cache.h b/cache.h
index 2711048..85ad594 100644
--- a/cache.h
+++ b/cache.h
@@ -654,6 +654,7 @@ extern int warn_on_object_refname_ambiguity;
 extern const char *apply_default_whitespace;
 extern const char *apply_default_ignorewhitespace;
 extern const char *git_attributes_file;
+extern const char *git_hooks_directory;
 extern int zlib_compression_level;
 extern int core_compression_level;
 extern int core_compression_seen;
diff --git a/config.c b/config.c
index 10b5c95..543de4e 100644
--- 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.hooksdirectory"))
+		return git_config_pathname(&git_hooks_directory, var, value);
+
 	if (!strcmp(var, "core.bare")) {
 		is_bare_repository_cfg = git_config_bool(var, value);
 		return 0;
diff --git a/environment.c b/environment.c
index 57acb2f..ffb5dec 100644
--- a/environment.c
+++ b/environment.c
@@ -31,6 +31,7 @@ const char *git_log_output_encoding;
 const char *apply_default_whitespace;
 const char *apply_default_ignorewhitespace;
 const char *git_attributes_file;
+const char *git_hooks_directory;
 int zlib_compression_level = Z_BEST_SPEED;
 int core_compression_level;
 int core_compression_seen;
diff --git a/run-command.c b/run-command.c
index 8c7115a..ae8e470 100644
--- a/run-command.c
+++ b/run-command.c
@@ -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_directory)
+		strbuf_addf(&path, "%s/%s", git_hooks_directory, 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-directory.sh b/t/t1350-config-hooks-directory.sh
new file mode 100755
index 0000000..556c1d3
--- /dev/null
+++ b/t/t1350-config-hooks-directory.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+test_description='Test the core.hooksDirectory configuration variable'
+
+. ./test-lib.sh
+
+test_expect_success 'set up a pre-commit hook in core.hooksDirectory' '
+	mkdir -p .git/custom-hooks .git/hooks &&
+	cat >.git/custom-hooks/pre-commit <<EOF &&
+#!$SHELL_PATH
+printf "%s" "." >>.git/PRE-COMMIT-HOOK-WAS-CALLED
+EOF
+	cat >.git/hooks/pre-commit <<EOF &&
+	chmod +x .git/hooks/pre-commit
+#!$SHELL_PATH
+printf "%s" "SHOULD NOT BE CALLED" >>.git/PRE-COMMIT-HOOK-WAS-CALLED
+EOF
+	chmod +x .git/custom-hooks/pre-commit
+'
+
+test_expect_success 'Check that various forms of specifying core.hooksDirectory work' '
+	test_commit no_custom_hook &&
+	git config core.hooksDirectory .git/custom-hooks &&
+	test_commit have_custom_hook &&
+	git config core.hooksDirectory .git/custom-hooks/ &&
+	test_commit have_custom_hook_trailing_slash &&
+	git config core.hooksDirectory "$PWD/.git/custom-hooks" &&
+	test_commit have_custom_hook_abs_path &&
+	git config core.hooksDirectory "$PWD/.git/custom-hooks/" &&
+	test_commit have_custom_hook_abs_path_trailing_slash &&
+    printf "%s" "...." >.git/PRE-COMMIT-HOOK-WAS-CALLED.expect &&
+    test_cmp .git/PRE-COMMIT-HOOK-WAS-CALLED.expect .git/PRE-COMMIT-HOOK-WAS-CALLED
+'
+
+test_done
-- 
2.1.3

             reply	other threads:[~2016-04-22 23:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-22 23:33 Ævar Arnfjörð Bjarmason [this message]
2016-04-23  0:13 ` [PATCH] hooks: Add ability to specify where the hook directory is SZEDER Gábor
2016-04-23  0:44   ` Ævar Arnfjörð Bjarmason
2016-04-24 21:18     ` [PATCH v2] " Ævar Arnfjörð Bjarmason
2016-04-25 14:19       ` Ævar Arnfjörð Bjarmason
2016-04-25 19:11       ` Johannes Sixt
2016-04-25 20:33       ` Junio C Hamano
2016-04-26 16:31         ` Ævar Arnfjörð Bjarmason
2016-04-26 19:16           ` Junio C Hamano
2016-04-26 19:19             ` Ævar Arnfjörð Bjarmason

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1461367997-28745-1-git-send-email-avarab@gmail.com \
    --to=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).