Skip to content

SASS applications with git pre commit hook

Abraham edited this page Aug 4, 2017 · 1 revision

The intention of this guide is to add a git hook in order to run rubocop and reek on any ruby application you want to force yourself to follow the rules.

First you need to run the following commands, under the ruby/rails project directory:

$ mv .git/hooks/pre-commit.sample .git/hooks/pre-commit
$ chmod +x .git/hooks/pre-commit

This will give the hook execution permissions, don't worry we have not done anything yet.

Next add the following code into your .git/hooks/pre-commit file:

#!/bin/sh

pass=true
RED='\033[1;31m'
GREEN='\033[0;32m'
NC='\033[0m'

echo "Running Linters:"

# Run rubocop and get the output and return code
scss_lint=$(docker-compose run --rm web scss_lint)
return_code=$?

if [ $return_code != 0 ]; then
	echo "$scss_lint\n"
	printf "\n${RED}SCSS Lint failed, not commit was made"
	pass=false
else
	printf "${GREEN}SCSS Lint passed.${NC}\n"
fi

# If you reach this point, everything was cool and means you are a good player
if $pass; then
	exit 0
fi

exit 1

And that is it, next time you try to commit something scss_lint will execute and if it breaks, you will not be able to commit.

The code above, was based on https://github.com/angular/material2/wiki/Pre-commit-hook-for-linters

Clone this wiki locally