Skip to content

Instantly share code, notes, and snippets.

@kei-p
Last active April 21, 2017 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kei-p/c00972b403e467f952c6e74bee9774ad to your computer and use it in GitHub Desktop.
Save kei-p/c00972b403e467f952c6e74bee9774ad to your computer and use it in GitHub Desktop.
git-hooks
#! /bin/sh
# Usage:
# $ curl -sL https://gist.github.com/kei-p/c00972b403e467f952c6e74bee9774ad/raw/git_hooks_installer | sh
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
cd `pwd`/`git rev-parse --show-cdup`
fi
hook_files="utils pre-commit pre-push"
for file_name in $hook_files
do
dst=".git/hooks/$file_name"
curl -L https://gist.github.com/kei-p/c00972b403e467f952c6e74bee9774ad/raw/$file_name > $dst 2> /dev/null
chmod +x $dst
echo "install $dst"
done
#!/bin/sh
source .git/hooks/utils
skip_if_needed
# rubocop check -->
check_coding_style() {
files=$(git diff --name-only --cached --diff-filter=AMRC | grep -e ".*\.rb" | paste -s -)
if [ -n "$files" ]; then
bundle exec rubocop $files -D
fi
if [ $? -ne 0 ]; then
exit 1
fi
}
check "coding style" check_coding_style
finish
#!/bin/sh
source .git/hooks/utils
skip_if_needed
# sawp files
check_swap_files() {
swap_files=`find . -name ".*.sw*"`
if [ -n "$swap_files" ]; then
echo "Failed! This project is contained swap files!!!"
echo "$swap_files"
exit 1
fi
}
check "swap files" check_swap_files
# debug code check -->
check_dev_code() {
keywords=(binding.pry debugger FIXME:)
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
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment