リポジトリにpushする前に最低限、
- debug用のコードが含まれていないこと
- swapファイルによる編集の勘違いがないか
- コードが規約上(rubocop)問題ないか
を徹底したかったので、gitのpre-pushを書いてみました。
$ chmod +x .git/hooks/pre-push
を忘れずに!
.git/hooks/utils
#!/bin/sh
check() {
echo "< Check $1 >"
eval $2
echo "=> OK!\n"
}
finish() {
echo 'Finish!!'
sleep 0.5
exit 0
}
skip_if_needed() {
if [ -n "$SKIP" ]; then
echo "Skip checker!!"
sleep 0.5
exit 0
fi
}
declare -F > /dev/null
.git/hooks/pre-push
#!/bin/sh
source ./utils
skip_if_needed
# sawp files check -->
check_swap_files() {
swap_files=`find . -name ".*.sw*"`
if [ -n "$swap_files" ]; then
echo "Failed! This project contain swap files!!!"
echo "$swap_files"
exit 1
fi
}
check "swap files" check_swap_files
# development code check -->
check_dev_code() {
keywords=(binding.pry debugger focus:)
for keyword in ${keywords[@]}; do
invalid_files=`find spec app lib -type f -exec grep -l $keyword {} \;`
if [ -n "$invalid_files" ]; then
echo "Failed! \"$keyword\" is contained these files!!!"
for file in $invalid_files; do
line_nums=`grep -n $keyword $file | cut -c 1`
for line_num in $line_nums; do
echo "\"$keyword\"\t in $file on line $line_num"
done
done
exit 1
fi
done
}
check "development code" check_dev_code
# rubocop check -->
check_coding_style() {
bundle exec rubocop -D
if [ $? -ne 0 ]; then
exit 1
fi
}
check "coding style" check_coding_style
finish