为什么默认情况下我的 Git 预提交挂钩不可执行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8598639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:16:42  来源:igfitidea点击:

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

gitgithooks

提问by Josh Smith

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 +xon my pre-commit hook to get it to work.

如果您在以下内容中看到接受的答案: Aggregating and uglifying javascript in a git pre-commit hook,您会看到我必须chmod +x在我的 pre-commit hook 上做一个才能让它工作。

Why is this not executable by Git by default?

为什么默认情况下这不能被 Git 执行?

回答by Dave Newton

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

因为文件默认是不可执行的;它们必须设置为可执行。

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

a中的示例文件git init都是可执行的;如果它被复制或重命名为非样本文件,它将保留原始文件的x标志。

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

将使用当前默认值创建新文件。在您的情况下,请使用以下命令查看这些默认值umask

$ umask
0022

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

默认情况下,u+x除非明确设置为,否则不会出现新文件。

回答by VonC

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

我必须chmod +x在我的预提交钩子上做一个才能让它工作

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)

问题是要意识到它首先是不可执行的。
使用 Git 2.15.x/2.16(2018 年第一季度)会更容易

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

请参阅Damien Marié ( ) 的提交 f805a00(2017 年 10 月 6 日(由Junio C Hamano合并-- --commit 130b512,2017 年 11 月 6 日)mdamien
gitster

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:

run-command: 当一个钩子被忽略时添加提示

当存在钩子但文件未设置为可执行文件时,git 将忽略该钩子。
现在这是沉默的,这可能会令人困惑

此提交添加此警告以改善情况:

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.

以允许启用/通过新设置被引入所述可执行标志禁用钩的旧用例:advice.ignoredHook

回答by Jakub Wagner

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 .bashrcor a file you source when you start your terminal. The story is below :)

就像附加答案一样,这里是函数,您可以用于初始化 git 存储库,它会自动使钩子可执行;您应该在.bashrc启动终端时将其放入或源文件中。故事如下:)

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.

和你一样,我也很生气。我不想记住每次初始化存储库时都必须使所有钩子都可执行。另外,当您使用子模块时,它们的钩子不在 中.git/hooks,而是在 中.git/modules/NameOfSubmodule/hooks,并且这些也应该成为可执行文件。