Skip to content

Instantly share code, notes, and snippets.

@ToniRib
Last active January 5, 2018 00:29
Show Gist options
  • Save ToniRib/d1606eb6c6433eeff69cb73de20df78e to your computer and use it in GitHub Desktop.
Save ToniRib/d1606eb6c6433eeff69cb73de20df78e to your computer and use it in GitHub Desktop.
Options for GSC main repo pre-commit hooks

Pre Commit Hook Options For Ruby Style Guide Violations

If you would like to use one of this options, perform the following:

  1. In whatever repo you want this turned on in, navigate to .git/hooks in the terminal.
  2. Run mv pre-commit.sample pre-commit to rename the sample file and activate it.
  3. Paste one of the following into that file and save.
  4. Feel free to edit anything you would like in your own hook! They aren't commited, so you're welcome to have a custom configuration.

All of these assume that rubocop is installed in the Gemfile and that a rubocop.yml file exists in the directory. At a minimum, rubocop must be installed. If you don't have a rubocop.yml file, the default configuration will be used.

Each of these only check against files that have been staged for the given commit.

There are 5 options:

  1. Ask about autocorrect, and fail if the commit has violations
  2. Always use autocorrect, and fail if the commit still has violations
  3. Never use autocorrect, and fail if the commit has violations
  4. Never use autocorrect, but never fail even if there are violations
  5. Always use autocorrect, but never fail even if there are still violations

Option 1 - Ask User About Autocorrect:

Useful for command line interfaces as it stops to ask the user if they want to run with autocorrect on before committing. Does not work with SourceTree or RubyMine.

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
cyan='\033[0;36m'
white='\033[1;37m'
NC='\033[0m'

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=cc79c00a6302a0b54840b6dee0a583a43f9c5427
fi

# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty

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

echo "${green}[Ruby Style][Info]: Checking Ruby Style for all files staged for this commit!${NC}"
echo "${cyan}[Ruby Style][Info]: If you would like to skip this check while committing, add --no-verify or -n to the git commit command${NC}"
echo "${yellow}[Ruby Style][Warning]: Style violations will prevent you from commiting. If you choose not to fix them, you will need the --no-verify flag${NC}"
echo ""
echo "${white}Before we get started, would you like to turn on autocorrect to correct simple style violations automatically [y/n]?${NC}"
read autocorrect

if [ -n "$FILES" ]
then
	echo "${green}[Ruby Style][Info]: Checking the following staged files:${NC}"
	echo "${green}[Ruby Style][Info]: ${FILES}${NC}"

	# Run rubocop on the staged files
	if [ "$autocorrect" = "" ]; then
    autocorrect='n'
  fi
  case $autocorrect in
      [Yy] )
			    echo "${green}[Ruby Style][Info]: Running rubocop with autocorrect!${NC}"
					bin/bundle exec rubocop -a ${FILES}
					;;
      [Nn] )
					echo "${green}[Ruby Style][Info]: Running rubocop without autocorrect!${NC}"
			    bin/bundle exec rubocop ${FILES}
					;;
  esac

	if [ $? -ne 0 ]; then
	  echo "${red}[Ruby Style][Error]: Please fix the issues and try to commit again, or commit with --no-verify if you do not think they need to be fixed${NC}"
	  exit 1
	fi
else
	echo "${green}[Ruby Style][Info]: No files to check${NC}"
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

exit 0

Option 2 - Always Use Autocorrect:

Works in any program since it does not as the user for input. Useful if you always want the autocorrect feature on.

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
cyan='\033[0;36m'
white='\033[1;37m'
NC='\033[0m'

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=cc79c00a6302a0b54840b6dee0a583a43f9c5427
fi

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

echo "${green}[Ruby Style][Info]: Checking Ruby Style for all files staged for this commit!${NC}"
echo "${cyan}[Ruby Style][Info]: If you would like to skip this check while committing, add --no-verify or -n to the git commit command${NC}"
echo "${yellow}[Ruby Style][Warning]: Style violations will prevent you from commiting. If you choose not to fix them, you will need the --no-verify flag${NC}"
echo "${green}[Ruby Style][Info]: Running rubocop with autocorrect!${NC}"

if [ -n "$FILES" ]
then
	echo "${green}[Ruby Style][Info]: Checking the following staged files:${NC}"
	echo "${green}[Ruby Style][Info]: ${FILES}${NC}"

  bin/bundle exec rubocop -a ${FILES}

	if [ $? -ne 0 ]; then
	  echo "${red}[Ruby Style][Error]: Please fix the issues and try to commit again, or commit with --no-verify if you do not think they need to be fixed${NC}"
	  exit 1
	fi
else
	echo "${green}[Ruby Style][Info]: No files to check${NC}"
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

exit 0

Option 3 - Never Use Autocorrect:

Works in any program since it does not as the user for input. Useful if you never want rubocop to correct violations for you.

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
cyan='\033[0;36m'
white='\033[1;37m'
NC='\033[0m'

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=cc79c00a6302a0b54840b6dee0a583a43f9c5427
fi

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

echo "${green}[Ruby Style][Info]: Checking Ruby Style for all files staged for this commit!${NC}"
echo "${cyan}[Ruby Style][Info]: If you would like to skip this check while committing, add --no-verify or -n to the git commit command${NC}"
echo "${yellow}[Ruby Style][Warning]: Style violations will prevent you from commiting. If you choose not to fix them, you will need the --no-verify flag${NC}"
echo "${green}[Ruby Style][Info]: Running rubocop without autocorrect!${NC}"

if [ -n "$FILES" ]
then
	echo "${green}[Ruby Style][Info]: Checking the following staged files:${NC}"
	echo "${green}[Ruby Style][Info]: ${FILES}${NC}"

  bin/bundle exec rubocop ${FILES}

	if [ $? -ne 0 ]; then
	  echo "${red}[Ruby Style][Error]: Please fix the issues and try to commit again, or commit with --no-verify if you do not think they need to be fixed${NC}"
	  exit 1
	fi
else
	echo "${green}[Ruby Style][Info]: No files to check${NC}"
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

exit 0

Option 4 - Never Fail The Commit (No Autocorrect):

Works in any program since it does not as the user for input. Useful if you never want to exit with a status of '1' (i.e. you never want to be prevented from committing due to style violations) but you still want to be informed about them after you commit.

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with a zero status no matter what, so it will never prevent you from
# committing.
#
# To enable this hook, rename this file to "pre-commit".
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
cyan='\033[0;36m'
white='\033[1;37m'
NC='\033[0m'

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=cc79c00a6302a0b54840b6dee0a583a43f9c5427
fi

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

echo "${green}[Ruby Style][Info]: Checking Ruby Style for all files staged for this commit!${NC}"
echo "${green}[Ruby Style][Info]: Style violations will NOT prevent you from commiting.${NC}"
echo "${green}[Ruby Style][Info]: Running rubocop without autocorrect!${NC}"

if [ -n "$FILES" ]
then
	echo "${green}[Ruby Style][Info]: Checking the following staged files:${NC}"
	echo "${green}[Ruby Style][Info]: ${FILES}${NC}"

  bin/bundle exec rubocop ${FILES}

	if [ $? -ne 0 ]; then
	  echo "${yellow}[Ruby Style][Warning]: The issues above were detected while you were committing, but did not prevent your commit${NC}"
	  echo "${yellow}[Ruby Style][Warning]: Please fix them in a follow-on commit if you deem it necessary${NC}"
	  exit 0
	fi
else
	echo "${green}[Ruby Style][Info]: No files to check${NC}"
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

exit 0

Option 5 - Never Fail The Commit (With Autocorrect):

Works in any program since it does not as the user for input. Useful if you never want to exit with a status of '1' (i.e. you never want to be prevented from committing due to style violations) but you still want to be informed about them after you commit. Includes autocorrect.

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with a zero status no matter what, so it will never prevent you from
# committing.
#
# To enable this hook, rename this file to "pre-commit".
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
cyan='\033[0;36m'
white='\033[1;37m'
NC='\033[0m'

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=cc79c00a6302a0b54840b6dee0a583a43f9c5427
fi

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

echo "${green}[Ruby Style][Info]: Checking Ruby Style for all files staged for this commit!${NC}"
echo "${green}[Ruby Style][Info]: Style violations will NOT prevent you from commiting.${NC}"
echo "${green}[Ruby Style][Info]: Running rubocop with autocorrect!${NC}"

if [ -n "$FILES" ]
then
	echo "${green}[Ruby Style][Info]: Checking the following staged files:${NC}"
	echo "${green}[Ruby Style][Info]: ${FILES}${NC}"

  bin/bundle exec rubocop -a ${FILES}

	if [ $? -ne 0 ]; then
	  echo "${yellow}[Ruby Style][Warning]: The issues above were detected while you were committing, but did not prevent your commit${NC}"
	  echo "${yellow}[Ruby Style][Warning]: Please fix them in a follow-on commit if you deem it necessary${NC}"
	  exit 0
	fi
else
	echo "${green}[Ruby Style][Info]: No files to check${NC}"
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment