【发布时间】:2016-04-16 04:57:09
【问题描述】:
我想阻止所有开发人员执行 git push to master(--force 除外)。我不想要求每个人在 .git/hooks 文件夹中创建一个预推送文件,而是想使用 npm 自动化该过程并将所需的文件添加到远程 git 存储库。
我在 package.json 中添加了指向自定义脚本的“预推送”,
"pre-push": {
"run": [
"prepushscript"
]
},
"scripts": {
"start": "node app.js",
"test": "node_modules/.bin/gulp test",
"prepushscript": "prepushscript"
},
"devDependencies": {
.
.
"pre-push": "^0.1.0",
.
.
}
注意:自定义脚本 prepushscript 与 package.json 处于同一文件级别。
prepushscript 文件相当简单
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]
then
echo "Error: You are attempting to push to a protected branch."
exit 1 # push will not execute
fi
自定义脚本有效,因为在 .git/hooks 文件夹中创建本地预推送文件并尝试推送到受保护的分支会导致看到脚本的回显错误消息。 Npm install 也会在 .git/hooks 文件夹中创建符号链接。一旦我准备好在本地主机上推送并运行 git push 的提交,推送就会通过,我得到以下信息,
pre-push:
pre-push: Failed to find the root of this git repository, cannot locate the `package.json`.
pre-push: Skipping the pre-push hook.
pre-push:
Everything up-to-date
我尝试用 package.json 中的简单 echo 语句替换 prepushscript 条目,以查看脚本是否有问题,但我得到了相同的控制台日志。进行的其他尝试是向 prepushscript 文件添加扩展名以查看它是否运行。
我无法弄清楚的主要问题是我无法找到错误的来源以及它与 package.json 的关系。感谢您的帮助!
【问题讨论】:
标签: git npm hook package.json