essay | tech | year-summary | about
日期:2019-01-07T00:00:00Z
我们都知道,在database中存在trigger,在一个图形界面中存在event。
那么在git中是否存在一个扳机呢?让我自由的触发事件呢?
答案是存在的。他就是git hooks。
我这里举几个例子把。
比如你的网站是典型的在github上面托管的网站。
现在移植到自己的服务器上面。发现好麻烦,不能用个push就热更新。
你想要一个push就热更新,不想要再连接到server,或者向server发送额外的信息。
这时你只需要使用git hooks里面的post-recieve就好了。
再比如,你由于工作需要,每次要在commit mesage上面加入固定的字组。
git hooks也能帮助你,编辑git hooks中的prepare-commit-msg就可以了。
参考资料的[1]中的分类,
它根据用途分为3大类,
这里使用post-recieve做例子。
只需要把post-recieve.example改名成为post-recieve就行了。
然后把post-recieve改为可执行的。
然后把代码写入post-recieve里面。
这里需要尤其注意,
如果要使用git pull的话,git会默认设置一个$GIT_DIR的数值,所以要
unset GIT_DIR一下才行。
如果不用这个代码,即便用-C也是没有用的。
一个非典型的trap。
下面是参考代码。
echo "recieved data correctly"
unset GIT_DIR
git pull -C your_path && echo git pull -C your_path
echo "update success"
很简单的小技巧,对不对?
updated:2022-1-17
it seems post-recieve is obsolated. use post-update instead.
git -C <dir> pull
yarn --cwd <dir> build
gulp --gulp <gulp_file> --cwd <dir>
[1]git-scm: 8.3 Customizing Git - Git Hooks
[2]git-scm: 10.8 Git Internals - Environment Variables
[3]stakoverflow: git post-receive not working correctly
[4]qiita: gitでpushと同時にデプロイする場合の注意