Why is my Git pre-commit hook not executable by default?

If you see the accepted answer in: Aggregating and fading javascript in git pre-commit hooks , you will see that I had to do chmod +x on my pre-commit hook to get it working.

Why is this not done with git by default?

+10
source share
3 answers

Because files are not executed by default; they must be installed as executable.

Sample files from git init are executed; if it is copied or renamed to a file without a sample, it will retain the original x flag.

New files will be created with the current default values. In your case, view these defaults using umask :

 $ umask 0022 

By default, new files will not be u+x unless explicitly installed.

+20
source

I had to run chmod +x on my pre-commit hook to get it working

The problem is to understand that it was not executable in the first place.
It will be easier with Git 2.15.x / 2.16 (Q1 2018)

See Commit f805a00 (October 6, 2017) by Damien mdamien ( mdamien ) .
(Combined with Junio ​​C Hamano - gitster - in commit 130b512 , November 06, 2017)

run-command : add hint when trap is ignored

When a trap is present, but the file is not set as executable, git will ignore the trap.
While it is silent, it can be confusing .

This commit adds this warning to improve the situation:

 hint: The 'pre-commit' hook was ignored because it not set as executable. hint: You can disable this warning with 'git config advice.ignoredHook false' 

To enable the old option of using on / off interceptors via the executable flag, a new parameter has been introduced: advice.ignoredHook .

+6
source

Just like an additional answer, here is a function that you can use to initialize a git repository that automatically makes executable traps; You must put it in .bashrc or in the file that you use when starting the terminal. The story is below :)

 ginit () { git init gitpath='git rev-parse --show-superproject-working-tree --show-toplevel | head -1' chmod u+x "$gitpath"/.git/hooks/* for submodule in "$gitpath"/.git/modules/*; do chmod u+x "$submodule"/hooks/* done } 

I was annoyed by the same as you. I don’t want to remember that I have to do all the trap executable files every time I initialize the repository. In addition, when you use submodules, their hooks are not in .git/hooks , but in .git/modules/NameOfSubmodule/hooks , and they must also be executable.

0
source

All Articles