目指すは完全自動化
gitのcommitでフォーマット自動整形とプログラムチェックを行う
.git/hooks/pre-commit
ファイルを作成する。
#!/bin/sh
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Redirect output to stderr.
exec 1>&2
IS_ERROR=0
# コミットされるファイルのうち、phpのみ対象とする。
for FILE in `git diff-index --name-status $against -- | grep -E '^[AUM].*\.php$'| cut -c3-`; do
# シンタックスのチェック
if php -l $FILE; then
# PSR準拠でコードを書き換える。
php-cs-fixer fix $FILE --rules=@PSR2
git add $FILE
# PHPMDで未使用変数などのチェックをする。
if ! phpmd $FILE text unusedcode,codesize,naming,controversial; then
IS_ERROR=1
fi
else
IS_ERROR=1
fi
done
exit $IS_ERROR
git commit時にチェックがかかる。
※途中だけどどうしてもチェックインしたい場合は--no-verify
オプションをつける
git commit -m "commit message" --no-verify
php-cs-fixerをインストールする。
windowsでgitコミット時にフォーマット自動整形を行う。
1.インストールする
composer global require friendsofphp/php-cs-fixer
2.パスを設定する
C:\Users{ユーザ名}\AppData\Roaming\Composer\vendor\bin 配下にパスを設定する。
phpmdをインストールする
1.インストールする
使用されていない変数やメソッドや、メソッド名(キャメルケース)などをチェックする。
コードを綺麗にするツール。
composer global require phpmd/phpmd=*
改行コードは LFに統一する
git config --global core.autocrlf input
inputを設定すると、commitはCRLFをLFに変換し、取得時は変換しない(LFのまま)となる。
globalに設定する。
git config --global --list
configを確認できる。
gitは3つのconfigを設定できる。
- system
- global
- local
インデントはタブ4つにする
リモートリポジトリはタブ4つに統一される。
1.attributesを作成し、対象ファイルを定義する。
.git/info
にattributes
を作成する。
*.js filter=tabspace
*.html filter=tabspace
*.sql filter=tabspace
対象とするファイルを列挙する。
2.commit時に変換するよう設定する。
git config --global filter.tabspace.smudge "unexpand --tabs=4 --first-only"
git config --global filter.tabspace.clean "expand --tabs=4"
コミットメッセージにissue noが書かれているかチェックする
コミットメッセージにissue noが書かれているかどうかチェックする。
issueがない場合は「no issue」と書く。
.git/hooks/commit-msg
を作成する。
#!/bin/sh
# Redirect output to stderr.
exec 1>&2
IS_ERROR=0
# メッセージを取得する。
msg=`cat $1`
if [[ $msg =~ ^#|^"no issue" ]] ;
then
:
else
echo "コメントはissueNo(#12)かno issue を先頭に記入すること"
IS_ERROR=1
fi
exit $IS_ERROR;