越來檢查有沒有符合該專案的 coding style.
所以可以叫 git commit 時,自動 run 這個 script。
防止不合style 的 code 被 commit 進 source code
這個動作通常是放在 git 的 hooks 裡:
.git/hooks/prepare-commit-msg
這個 hooks 是一個 shell script,在 git commit 時,書入完 commit 時會被執行。
所以可以這樣寫:
#!/bin/sh if test -r "checkpatch.pl"; then git diff --cached | ./checkpatch.pl --no-signoff --no-tree - fi檢查project 目錄是不是有 checkpatch.pl 好決定是不是要 執行
這樣改完後,如果 checkpatch.pl 結果是 warning 或 error, commit 就不會進行 commit
這個 script 也可以吃參數,$1 就是 commit comment。
所以可以利用 commit comment 來控制他的動作。
例如:
! grep -qs "\[NO_STYLE_CHECK\]" "$1" || exit 0就可以在 commit 時,加入 "[NO_STYLE_CHECK]" bypass 掉 checkpatch:
$ git commit -am "[NO_STYLE_CHECK] try commit dirty codes"
ref:
- http://blog.bartoszmajsak.com/blog/2012/11/07/lazy-developers-toolbox-number-1-prepend-git-commit-messages/
- https://answers.atlassian.com/questions/169399/sourcetree-and-git-prepare-commit-msg
- http://www.fluff.org/ben/linux/githooks/pre-commit
#!/bin/sh # # pre-commit hook to run check-patch on the output and stop any commits # that do not pass. Note, only for git-commit, and not for any of the # other scenarios # # Copyright 2010 Ben Dooks,if git rev-parse --verify HEAD 2>/dev/null >/dev/null then against=HEAD else # Initial commit: diff against an empty tree object against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 fi git diff --cached $against -- | ./scripts/checkpatch.pl --no-signoff -