在git
也有跟webhook類似的強大功能Git Hooks
,是在git action觸發的同時去進行一些腳本。
Hook 名稱 | 觸發指令 |
---|---|
applypatch-msg | git am |
pre-applypatch | git am |
post-applypatch | git am |
pre-commit | git commit |
prepare-commit-msg | git commit |
post-commit | git commit |
post-checkout | git checkout and git clone |
post-merge | git merge or git pull |
pre-push | git push |
pre-receive | git-receive-pack on the remote repo |
update | git-receive-pack on the remote repo |
post-update | git-receive-pack on the remote repo |
post-rewrite | git commit --amend, git-rebase |
在commit之前觸發上自動幫你透過builder製作proto檔案,並自動git add build完成的files。
edit proto file -> git add proto file -> git commit -> trigger git hook 「pre-commit」->
docker run proto-builder -> 檢查有無異動proto -> git add .pb.go files -> git commit finish !
所以我們今天要建立的git hook是pre-commit
建立pre-commit的shellscriptadd-pre-commit.sh
#/bin/sh
echo '
#/bin/sh
echo "proto builder start"
docker run -v $(pwd):/go/src/app -w /go/src/app rain123473/proto-builder:g1.14-p3.12.0 sh -c "protoc *.proto --go_out=plugins=grpc:.;"
if [[ ! $(git diff --name-only) ]]; then
echo "Needless To add"
else
git add *.pb.go
fi
'>.git/hooks/pre-commit
chmod 775 .git/hooks/pre-commit
然後在該目錄執行
sh add-pre-commit.sh
查看有無新增.git/hooks/pre-commit
cat .git/hooks/pre-commit
有顯示下列 script內容即已經綁定好pre-commit
囉
#/bin/sh
echo "proto builder start"
docker run -v $(pwd):/go/src/app -w /go/src/app rain123473/proto-builder:g1.14-p3.12.0 sh -c "protoc *.proto --go_out=plugins=grpc:.;"
if [[ ! $(git diff --name-only) ]]; then
echo "Needless To add"
else
git add *.pb.go
fi
這樣以後此proto的編輯者,只需專注於proto檔案的編輯,再也不用在擔心build的問題囉~