We\'d like to make a few basic hook scripts that we can all share -- for things like pre-formatting commit messages. Git has hook scripts for that that are normally stored
I found these scripts very useful for gradle projects.
apply from: rootProject.file('gradle/install-git-hooks.gradle')
tasks.create(name: 'gitExecutableHooks') {
doLast {
Runtime.getRuntime().exec("chmod -R +x .git/hooks/");
}
}
task installGitHooks(type: Copy) {
from new File(rootProject.rootDir, 'pre-commit')
into { new File(rootProject.rootDir, '.git/hooks') }
}
gitExecutableHooks.dependsOn installGitHooks
clean.dependsOn gitExecutableHooks
.... your pre commit scripts goes here
In Git 2.9, the configuration option core.hooksPath specifies a custom hooks directory.
Move your hooks to a hooks
tracked directory in your repository. Then, configure each instance of the repository to use the tracked hooks
instead of $GIT_DIR/hooks
:
git config core.hooksPath hooks
In general, the path may be absolute, or relative to the directory where the hooks are run (usually the working tree root; see DESCRIPTION section of man githooks).
Most of the modern programming languages, or rather their build tools, support plugins to manage git hooks. That means all you need to do is configure your package.json, pom.xml, etc., and anyone in your team will have no option but to comply unless they change the build file. The plugin will add content to .git directory for you.
Examples:
https://github.com/rudikershaw/git-build-hook
https://github.com/olukyrich/githook-maven-plugin
https://www.npmjs.com/package/git-hooks
You can make your hooks folder another git repository and link it as a submodule... I guess worth it only if you have a lot of members and hooks changed regularly.
If your project is a JavaScript project and you use npm
as package manager you can use shared-git-hooks to enforce githooks on npm install
.
For Nodejs users a simple solution is to update package.json with
{
"name": "name",
"version": "0.0.1",
......
"scripts": {
"preinstall": "git config core.hooksPath hooks",
The preinstall will run before
npm install
and redirects git to look for hooks inside the .\hooks (or whatever name you choose) directory. This directory should mimic .\.git\hooks in terms of file name (minus the .sample) and structure.
Imagine Maven and other build tools will have an equivalent to preinstall.
It should also work across all platforms.
If you need any more info see https://www.viget.com/articles/two-ways-to-share-git-hooks-with-your-team/