How can I add my pre-commit hook to an existing git repository

可紊 提交于 2020-02-20 06:46:30

问题


I have a pre-commit hook and I want to add it to the repository so that by checking it out my colleagues have it instantly in place.
However if I try to add it ( being in the root directory of my project) I get the following result:

$ git add  .git/hooks/pre-commit
error: Invalid path '.git/hooks/pre-commit'
error: unable to add .git/hooks/pre-commit to index

Any idea if this work and how to achieve my goal?


回答1:


checking it out my colleagues have it instantly in place

Sept. 2015: That is not possible: a hook can be put in source control (you simply copy the script in your git repo), but it cannot be "automatically in place" (active) on clone/checkout: that would be way too dangerous, depending on what the hook is actually doing.
See also "Git remote/shared pre-commit hook"

You would still need to activate it, by adding a symlink (even on Windows) to the pre-commit hook present in the git repo.

Update Dec. 2016: the OP Calamity Jane mentions in the comments:

I solved it by now in symfony2 projects (and with others, it also should work) to have it as part of the composer.json.
So if a colleague is doing a composer install or composer update, it is automatically placed in the correct place.

"scripts": { "dev-install": [ "bash setup_phpcs.sh" ] }, 

So on dev, setup_phpcs.sh is automatically called and that copies the hook from a folder in the repository to the right place.
And since the hook is part of the repository it can be easily updated and distributed.

As noted by Hi-Angel in the comments:

I figured that out: Emacs has build-aux dir with hooks, and, upon running autogen.sh, all hooks are copied from there.




回答2:


fyi information in the year 2019 there is also this option:

require this package: "brainmaestro/composer-git-hooks"

and add the following lines to your composer.yaml

"extra": {
    "hooks": {
        "commit-msg": [
            "regex=\"^([A-Z]{2,4}-[0-9]{1,4}|(no-ticket|NO-TICKET)):[\\s]*.{10,}\"",
            "file=`cat $1`",
            "if ! [[ $file =~ $regex ]]; then",
            "  echo \"ERROR - Commit message is wrong or too short. E.g. XXX-33: Description or no-ticket : Description\"",
            "  exit 1",
            "fi"
        ],
        "pre-commit": [
            "git status --porcelain | grep -e '^ [AM]\\(.*\\).php$' | cut -c 3- | while read line; do",
            "ROOT=`php -r \"echo __DIR__;\"`",
            "bin/php-cs-fixer fix -nq --config=$ROOT/.php_cs \"$line\";",
            "bin/phpcbf --standard=PSR2 --encoding=utf-8 -n -p \"$line\";",
            "git add \"$line\";",
            "done",
            "echo committing on branch $(git rev-parse --abbrev-ref HEAD)"
        ]
    }
}

This is the example, which works for me. What it basically does is this:

Everytime you run "composer install" or "composer update" the hooks in .git/hooks are checked. If the hooks are already in place nothing happens. If they are missing, then the lines from above are parsed into the the hooks with a shebang at the beginning. They are then executed each time somebody is triggering a hook.

If you don't have big scripts that is IMO the better solution than copying scripts around.

Note: if you change the lines in the composer.json for the hooks you have to delete the respective hook first before you run "composer install" or nothing will change.



来源:https://stackoverflow.com/questions/32649628/how-can-i-add-my-pre-commit-hook-to-an-existing-git-repository

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