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

怎甘沉沦 提交于 2019-12-18 19:17:44

问题


If you see the accepted answer in: Aggregating and uglifying javascript in a git pre-commit hook, you'll see that I had to do a chmod +x on my pre-commit hook to get it to work.

Why is this not executable by Git by default?


回答1:


Because files are not executable by default; they must be set to be executable.

The sample files from a git init are all executable; if it's copied or renamed to a non-sample file, it will retain the original file's x flag.

New files will be created with current defaults. In your case, view those defaults with umask:

$ umask
0022

By default, new files won't be u+x unless explicitly set to be.




回答2:


I had to do a chmod +x on my pre-commit hook to get it to work

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

See commit f805a00 (06 Oct 2017) by Damien Marié (mdamien).
(Merged by Junio C Hamano -- gitster -- in commit 130b512, 06 Nov 2017)

run-command: add hint when a hook is ignored

When an hook is present but the file is not set as executable then git will ignore the hook.
For now this is silent which can be confusing.

This commit adds this warning to improve the situation:

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

To allow the old use-case of enabling/disabling hooks via the executable flag a new setting is introduced: advice.ignoredHook.




回答3:


Just as add-on answer, here is the function, you can use for initializing a git repository, which automatically makes hooks executables; you should put it in .bashrc or a file you source when you start your 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 thing as you. I do not want to remember that I have to make all hooks executables every time I initialize a repository. Plus, when you use submodules, their hooks are not in .git/hooks, but in .git/modules/NameOfSubmodule/hooks, and these should be made executables too.



来源:https://stackoverflow.com/questions/8598639/why-is-my-git-pre-commit-hook-not-executable-by-default

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