Skip to content

Instantly share code, notes, and snippets.

@jonnovaretti
Created January 25, 2024 15:25
Show Gist options
  • Save jonnovaretti/22ffa1a7b54888e3e229acf8d70da383 to your computer and use it in GitHub Desktop.
Save jonnovaretti/22ffa1a7b54888e3e229acf8d70da383 to your computer and use it in GitHub Desktop.
pre-commit
#!/bin/sh
# ------------------------------------------------------------------------------------#
# console log check on changed files
# debugger check on changed files
# merge chars check on changed files
# ------------------------------------------------------------------------------------#
# 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 'console.log - Continuing...'
else
exit 1;
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
echo 'merge marker check 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
changed=$(git diff --cached --name-only)
if [[ -z "$changed" ]]
then
echo 'debugger Continue...'
else
echo $changed | xargs egrep 'debugger' -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 debugger 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