Update pre_commit hook file on version control

杀马特。学长 韩版系。学妹 提交于 2020-01-02 06:57:25

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/40550477/update-pre-commit-hook-file-on-version-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!