有了这个 MWE,Git 在错误的存储库 (A
) 中执行 Hook :
# Make simple repo
git init "/tmp/A" && cd "/tmp/A" || exit 1
# Make hook to demonstrate the problem
mkdir -p ".git/hooks" &&
echo "echo 'EXECUTED!!!'" >.git/hooks/reference-transaction &&
chmod +x ".git/hooks/reference-transaction" &&
git clone -c core.hooksPath=".git/hooks" --template= "https://github.com/gabyx/Githooks.git" /tmp/Githooks
结果
Initialized empty Git repository in /home/test/tmp/A/.git/
Cloning into 'Githooks'...
EXECUTED!!!
EXECUTED!!!
EXECUTED!!!
EXECUTED!!!
这很奇怪:我希望 git clone
执行 /tmp/Githooks/.git/hooks
中的钩子(Hook),因为来自
The path can be either absolute or relative. A relative path is taken as relative to the directory where the hooks are run (see the "DESCRIPTION" section of githooks[5]).
并进一步在Doc :
Before Git invokes a hook, it changes its working directory to either $GIT_DIR in a bare repository or the root of the working tree in a non-bare repository.
这是为什么,这是一个应该报告的错误吗?
最佳答案
我认为这是一个错误。
git clone
命令是一种特殊情况,因为它创建了一个新的 Git 存储库。后续操作应该发生在这个新存储库中。相对路径应该 是新存储库的相对路径。但是,除非模板提供 Hook ,否则那里不应有 Hook 。
(我不清楚模板可以是 Git 存储库:模板应该是本地文件树。)
作为一般规则,运行任何还没有机会先被验证的钩子(Hook)是不明智的。
这是会产生争论的情况之一。
关于git - 为什么 Git 执行来自其他存储库的钩子(Hook)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67273420/