Git Hooks to ease YOUR Development Flow !!!!

In this tutorial we will  understanding a little about git hooks and how they can be made part of our development flow with some automation

Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customize Git’s internal behavior and trigger customizable actions at key points in the development life cycle.

Installing Hooks

Hooks reside in the .git/hooks directory of every Git repository. Git automatically populates this directory with example scripts when you initialize a repository. If you take a look inside .git/hooks, you’ll find the following files:

applypatch-msg.sample       pre-push.sample
commit-msg.sample           pre-rebase.sample
post-update.sample          prepare-commit-msg.sample
pre-applypatch.sample       update.sample
pre-commit.sample

These represent most of the available hooks, but the .sampleextension prevents them from executing by default. To “install” a hook, all you have to do is remove the .sample extension. Or, if you’re writing a new script from scratch, you can simply add a new file matching one of the above filenames, minus the .sampleextension.

This is a simple  pre commit Git Hook

It runs rubocop only against the staged files and it will not commit if it finds offences.

The pre-commit is a simple bash script

#check for ruby style errors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
BCYAN='\033[1;36m'

echo -e "${BCYAN}Running pre-commit hook, to run with out do 'git commit -n' ${NC}"

# Check if rubocop is installed for the current project
bin/bundle exec rubocop -v >/dev/null 2>&1 || { echo -e >&2 "${RED}[Ruby Style][Fatal]: Add rubocop to your Gemfile${NC}"; exit 1; }

# Get only the staged files
FILES="$(git diff --cached --name-only --diff-filter=AMC | grep "\.rb$" | tr '\n' ' ')"

echo -e "${GREEN}[Ruby Style][bundle]: Checking Ruby Style${NC}"

if [ -n "$FILES" ]
then
echo -e "${GREEN}[Ruby Style][Info]: ${FILES}${NC}"

# Run rubocop on the staged files
bin/bundle exec rubocop ${FILES}

if [ $? -ne 0 ]; then
echo -e "${BCYAN}✋ You've Failed offenses!!!${NC}"
echo -e "${RED}[Ruby Style][Error]: Fix the issues and commit again${NC}"
echo -e "${BCYAN}------------------------------------------------------${NC}"
echo -e "${YELLOW}[Rubocop][Info]: To Log the results in yml file run 'rubocop --auto-gen-config' this will run the commpond on entire project, to run on file specify filename ${NC}"
echo -e "${BCYAN}EXAMPLE-- rubocop app/models/user.rb --auto-gen-config${NC}"

exit 1
else
echo -e "${GREEN}[Ruby Style][Info]: ${FILES} --> 👍 approved.${NC}"
fi
else
echo -e "${GREEN}[Ruby Style][Info]: No files to check${NC}"
fi

exit 0

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
BCYAN='\033[1;36m'

echo -e "${BCYAN}Running pre-commit hook, to run with out do 'git commit -n' ${NC}"

# Check if rubocop is installed for the current project
bin/ exec rubocop -v >/dev/null 2>&1 || { echo -e >&2 "${RED}[Ruby Style][Fatal]: Add rubocop to your Gemfile${NC}"; exit 1; }

# Get only the staged files
FILES="$(git diff --cached --name-only --diff-filter=AMC | grep "\.rb$" | tr '\n' ' ')"

echo -e "${GREEN}[Ruby Style][bundle]: Checking Ruby Style${NC}"

if [ -n "$FILES" ]
then
echo -e "${GREEN}[Ruby Style][Info]: ${FILES}${NC}"

# Run rubocop on the staged files
bin/bundle exec rubocop ${FILES}

if [ $? -ne 0 ]; then
echo -e "${BCYAN}✋ You've Failed offenses!!!${NC}"
echo -e "${RED}[Ruby Style][Error]: Fix the issues and commit again${NC}"
echo -e "${BCYAN}------------------------------------------------------${NC}"
echo -e "${YELLOW}[Rubocop][Info]: To Log the results in yml file run 'rubocop --auto-gen-config' this will run the commpond on entire project, to run on file specify filename ${NC}"
echo -e "${BCYAN}EXAMPLE-- rubocop app/models/user.rb --auto-gen-config${NC}"

exit 1
else
echo -e "${GREEN}[Ruby Style][Info]: ${FILES} --> 👍 approved.${NC}"
fi
else
echo -e "${GREEN}[Ruby Style][Info]: No files to check${NC}"
fi

exit 0

Copy gist above and paste it in your project’s hook directory. Save the file as pre-commit (your-project/.git/hooks/pre-commit)

Change the pre-commit permissions with chmod +x your-project/.git/hooks/pre-commit

Happy Codding ….!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s