是否可以使用赫斯基(Husky)设置预推钩,以防止误将其推入母带? 赫斯基文档非常差,所以我找不到答案。
现在,我已经设置了像这样的哈士奇:
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"pre-push": "npm run lint"
}
},
"lint-staged": {
"linters": {
"*.{js,json,scss,md}": [
"prettier --write",
"git add"
],
"*.js": [
"eslint -c .eslintrc --fix",
"echo test",
"git add"
]
}
}
答案 0 :(得分:1)
尝试将您的预推钩子更改为:"npm run lint && git branch | grep \"*\" | egrep -v \"^* master$\""
当当前分支为master时,这将导致git push
失败
答案 1 :(得分:0)
我所做的是制作一个预推式bash脚本并将其提交到存储库中。然后从带有husky参数的husky预推钩中调用此脚本。
这是我在package.json中的多壳配置(如果需要,可以设置单独的配置)
"husky": {
"hooks": {
"pre-commit": "./commands/pre-commit",
"pre-push": "./commands/pre-push $HUSKY_GIT_STDIN"
}
},
如您所见,我有2个脚本,一个用于预推脚本,一个用于预提交脚本。
这是我的commands/pre-push
脚本
#!/bin/bash
echo -e "===\n>> Talenavi Pre-push Hook: Checking branch name / Mengecek nama branch..."
BRANCH=`git rev-parse --abbrev-ref HEAD`
PROTECTED_BRANCHES="^(master|develop)"
if [[ $1 != *"$BRANCH"* ]]
then
echo -e "\n? You must use (git push origin $BRANCH) / Anda harus menggunakan (git push origin $BRANCH).\n" && exit 1
fi
if [[ "$BRANCH" =~ $PROTECTED_BRANCHES ]]
then
echo -e "\n? Cannot push to remote $BRANCH branch, please create your own branch and use PR."
echo -e "? Tidak bisa push ke remote branch $BRANCH, silahkan buat branch kamu sendiri dan gunakan pull request.\n" && exit 1
fi
echo -e ">> Finish checking branch name / Selesai mengecek nama branch.\n==="
exit 0
该脚本基本上可以完成两件事:
master
和develop
分支)。他们需要在自己的分支中工作,然后创建拉取请求。fix/someissue
中,但随后错误地键入了git push origin master
。有关更多详细说明,请参见本文:
https://github.com/talenavi/husky-precommit-prepush-githooks