Created
March 27, 2017 23:57
-
-
Save yaoyunchen/32668014ca8573b66994bfe161770b25 to your computer and use it in GitHub Desktop.
Git Hooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Git Hooks for Rails | |
# Runs automatically depending on what you're doing with git. | |
# Add to ./bin | |
# PRE-COMMIT HOOKS | |
# Automatically runs Codeclimate checks before a commit. | |
cat >".git/hooks/pre-push" <<EOL | |
#!/bin/bash | |
red='\033[0;31m' | |
green='\033[0;32m' | |
yellow='\033[0;33m' | |
cyan='\033[1;36m' | |
NC='\033[0m' | |
printf "\${cyan}Checking for Docker..." | |
docker ps 1> /dev/null 2>&1 | |
if [ \$? -ne 0 ]; then | |
printf "\${red}Check that docker is running correctly\n" | |
exit 1 | |
else | |
printf "\${green}Docker running...\n" | |
fi | |
# Check if codeclimate image exists. If not, pull it. | |
printf "\${cyan}Checking for Codeclimate image..." | |
if docker images -q codeclimate/codeclimate >/dev/null 2>&1; then | |
printf "\${green}Codeclimate image exists.\n" | |
else | |
printf "\${red}Codeclimate image does not exist. Refer to README on how to add Codeclimate.\n" | |
exit 1 | |
fi | |
# Check if .codeclimate.yml exists. | |
if [ ! -f '.codeclimate.yml' ]; then | |
printf "\${red} No .codeclimate.yml config file.\n" | |
exit 1 | |
else | |
printf "\${green}.codeclimate.yml found.\n" | |
fi | |
printf "\${cyan}If this is the first time it may take a while as it needs to pull dependent images.\n" | |
# Analyze code with codeclimate and check for errors. | |
docker run --tty --rm --env CODE_PATH="\$PWD" --volume "\$PWD":/code --volume "/tmp/cc:/tmp/cc" --volume /var/run/docker.sock:/var/run/docker.sock codeclimate/codeclimate analyze 2>&1 | tee -a >(sed -E "s/"$'\E'"\[([0-9]{1,2}(;[0-9]{1,2})*)?m//g" >codeclimate_logs) | |
# Check how many errors there were. | |
awk 'NF{p=\$0}END{print p}' codeclimate_logs | grep "Analysis complete! Found 0 issues." | |
if [ \$? -ne 0 ]; then | |
printf "\${red}Fix the issues and run the command again. Errors can be viewed at ./codeclimate_logs.\${NC}\n" | |
exit 1 | |
fi | |
exit 0 | |
EOL | |
# Change permissions for git hook pre-push file so it can be updated. | |
chmod +x '.git/hooks/pre-push' | |
echo -e "\033[0;32mGit pre-push hook at .git/hooks/pre-push updated.\033[0m" | |
# POST-MERGE HOOKS | |
# Runs every time a specified file changed when merging. | |
cat>".git/hooks/post-merge" <<EOL | |
#!/bin/bash | |
changed_files="\$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)" | |
check_run() { | |
echo "\$changed_files" | grep --quiet "\$1" && eval "\$2" | |
} | |
# ADD check_run [FILE_THAT_CHANGED] [COMMAND_TO_RUN] HERE | |
# Checks if docker files changed and run 'docker-compose build' if it did. | |
printf "Checking if docker-compose build required....\n" | |
check_run docker-compose.yml "docker-compose build" | |
check_run Dockerfile "docker-compose build" | |
for filename in "docker/*/*" | |
do | |
check_run "\${filename}" "docker-compose build" | |
done | |
# Check if the git-hooks file changed and needs to be updated. | |
check_run "bin/git-hooks" "bin/git-hooks" | |
EOL | |
# Change permissions for git hook post-merge file so it can be updated. | |
chmod +x '.git/hooks/post-merge' | |
echo -e "\033[0;32mGit post-merge hook at .git/hooks/post-merge updated.\033[0m" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment