Share precommit hook
原文链接 https://morefreeze.github.io/2016/05/share-precommit-hook.html
注:以下为加速网络访问所做的原文缓存,经过重新格式化,可能存在格式方面的问题,或偶有遗漏信息,请以原文为准。
{% include JB/setup %}
Today I want to share a code snippet. It used for reminding me run rake preview
before git commit
.
<!--more-->
TL;DR. Because I use jeklly
to generate my blog static pages, jeklly
is a generator of static blog.
I can write a markdown file (just like this one) and run rake preview
, jeklly
could help me
generate the html files from markdown. So I must remeber run this command before commit changes.
But I often review my articles and found some mistakes in them, I make a tiny hotfix and commit quickly.
In this case, I forgot to re-generate pages. So I made a hook before git commit
which runs this command
automatically and detects some changes occur. The hook will failed when I forget run the command.
Here it is:
./_gen_wiki.sh # My blog contains a wiki, so it need be generated.
jekyll build
git diff --quiet --exit-code _site/ # Detect uncached changes.
ret=$?
exec 1>&2
RED='\033[0;31m'
NC='\033[0m' # No Color
[[ x$ret != "x0" ]] && echo "${RED}Some files still not be stage in directory _site${NC}"
exit $ret