Richlewis
Richlewis

Reputation: 15374

Update pre_commit hook file on version control

I recently updated my pre_commit.sample file to run rubocop upon a commit (renamed to pre_commit).

#!/bin/sh
#
# Check for ruby style errors
rake run:rubocop

Mistakingly I thought this would update on other developers machines when pulling the changes. How can i ensure that anyone who pulls the change has their pre_commit file updated

Thanks

Upvotes: 6

Views: 3417

Answers (2)

VonC
VonC

Reputation: 1324537

As I mentioned in "Git commit hooks - global settings" and "change default git hooks" last May 2016, Git 2.9 and 2.10 introduced git config core.hooksPath.

That seems to confuse the OP:

If I have an existing repo and want all other dev's who pull changes to have an updated pre-commit hook for example how would I do this ?
Within the repo there is /.git/hooks/pre_commit, can I point it to that

More precisely, within a git repo, there is a /.git/hooks/pre-commit.sample and, considering you want a common pre-commit hook for all developers, you should not make and then point to a /.git/hooks/pre_commit script within your local repo.

All developers have to reference the same gobal shared network-accessible path used with git config core.hooksPath \\a\common\shared\path: that setting needs to be activated on each developer workstation, either within their repo (local setting) or for all their repo (global setting: git config --global core.hooksPath \\a\common\shared\path.

(I use here a Windows UNC syntax, use the one suited for your OS)

Once they all reference the same path, you can set up your pre-commit hook there:

 \\a\common\shared\path\pre-commit

Then you can update that script (the one accessed by everyone), allowing all developers to benefit instantly of the updates.

Upvotes: 2

CodeWizard
CodeWizard

Reputation: 142114

Up until git v2.9 there was no way to update hooks on client side.

Git v2.9 exposed new configuration to allow this with this:

git config [--local] core.hooksPath ...

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.

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.

Upvotes: 9

Related Questions