git hook分支_编写一个git post-receive hook来处理特定的分支

本文介绍了一个自定义的 Git pre-commit hook 脚本,该脚本会在非 master 分支提交时运行 husky 脚本进行代码检查。通过获取当前分支名并判断是否处于 master 分支来决定是否执行预提交检查。

来自@pauljz的答案适用于某些git钩子,如pre-push,但pre-commit无法访问这些变量oldrev newrev refname

所以我创建了这个替代版本,适用于预提交,或者真正和钩子。这是一个pre-commit挂钩,如果我们不在master分支上,它将运行husky脚本。

#!/bin/bash

# git 'commit' does not have access to these variables: oldrev newrev refname

# So get the branch name off the head

branchPath=$(git symbolic-ref -q HEAD) # Something like refs/heads/myBranchName

branch=${branchPath##*/} # Get text behind the last / of the branch path

echo "Head: $branchPath";

echo "Current Branch: $branch";

if [ "master" != "$branch" ]; then

# If we're NOT on the Master branch, then Do something

# Original Pre-push script from husky 0.14.3

command_exists () {

command -v "$1" >/dev/null 2>&1

}

has_hook_script () {

[ -f package.json ] && cat package.json | grep -q "\"$1\"[[:space:]]*:"

}

cd "frontend" # change to your project directory, if .git is a level higher

# Check if precommit script is defined, skip if not

has_hook_script precommit || exit 0

# Node standard installation

export PATH="$PATH:/c/Program Files/nodejs"

# Check that npm exists

command_exists npm || {

echo >&2 "husky > can't find npm in PATH, skipping precommit script in package.json"

exit 0

}

# Export Git hook params

export GIT_PARAMS="$*"

# Run npm script

echo "husky > npm run -s precommit (node `node -v`)"

echo

npm run -s precommit || {

echo

echo "husky > pre-commit hook failed (add --no-verify to bypass)"

exit 1

}

fi我希望能有所帮助。您可以轻松修改if和fi语句之间的任何需求。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值