Skip to content

Instantly share code, notes, and snippets.

@jarodsmk
Created June 1, 2018 07:39
Show Gist options
  • Save jarodsmk/5d459b279803e83f13ffb0c066da61c7 to your computer and use it in GitHub Desktop.
Save jarodsmk/5d459b279803e83f13ffb0c066da61c7 to your computer and use it in GitHub Desktop.
Precommit hook for merge markers and console.log
#!/bin/sh
# ------------------------------------------------------------------------------------#
# Console log check
# ------------------------------------------------------------------------------------#
# Redirect output to stderr.
exec 1>&2
# Enable user input
# exec < /dev/tty
# Updated regexp to only look at the addition of console.log's on the current branch HEAD
# thanks @brugnara!
consoleregexp='^\+.*console\.log('
# Check
if test $(git diff --cached | grep $consoleregexp | wc -l) != 0
then
exec git diff --cached | grep -ne $consoleregexp
read -p "There are some occurrences of console.log at your modification. Are you sure want to continue? (y/n)" yn
echo $yn | grep ^[Yy]$
if [ $? -eq 0 ]
then
echo "\n Cool, continuing..."
else
exit 1; # Halt
fi
fi
# ------------------------------------------------------------------------------------#
# Merge conflict marker checker
# ------------------------------------------------------------------------------------#
# Taken from: https://jondowdle.com/2015/02/block-merge-conflicts-in-commits/
## pre-commit script to prevent merge markers from being committed.
## Author: Jon Dowdle <jdowdle@gmail.com>
##
## This simply searches the files that you are about to commit.
##
changed=$(git diff --cached --name-only)
if [[ -z "$changed" ]]
then
exit 0; # Continue
else
echo $changed | xargs egrep '[><]{7}' -H -I --line-number
## If the egrep command has any hits - echo a warning and exit with non-zero status.
if [ $? == 0 ]
then
echo "\n\nWARNING: You have merge markers in the above files, lines. Fix them before committing.\n\n"
exit 1;
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment