Skip to content

Instantly share code, notes, and snippets.

@xezrunner
Last active March 8, 2024 05:38
Show Gist options
  • Save xezrunner/e6dbafcc21fcbc976c93bdee0f371a08 to your computer and use it in GitHub Desktop.
Save xezrunner/e6dbafcc21fcbc976c93bdee0f371a08 to your computer and use it in GitHub Desktop.
Git nocheckin pre-commit hook (with error message and grep output)
#!/bin/bash
# Author: xezrunner (github.com/xezrunner)
# Credit: DustinGadal on r/Jai
# https://www.reddit.com/r/Jai/comments/jp0vjy/nocheckin_behavior_in_gitsourcetree/gbfhzd1/
# Required programs/utilities for default behavior (as-is):
# git, grep, xargs
# This pre-commit hook/script checks for the existence of the word "$SEARCH_TARGET"
# in your *staged* source files, then aborts the commit if any matches were found.
# It also shows where you have them inside the file.
SEARCH_TARGET="nocheckin"
CL_RED='\e[31m'
CL_BRED='\e[1;31m'
CL_NONE='\e[0m'
MESSAGE_0="${CL_BRED}Error:${CL_NONE} $SEARCH_TARGET(s) were found in "
MESSAGE_1="file(s) - ${CL_BRED}ignoring commit:${CL_NONE}"
SEARCH_CMD="git diff --staged -i --diff-filter=d --name-only -G $SEARCH_TARGET --relative $PWD"
GREP_CMD="grep -H $SEARCH_TARGET -n --color=always" # <filename>:line******
# Get the amount of files that we found the search target in.
# I use 'wc -l' (line count of command output) for this:
STATUS=$($SEARCH_CMD | wc -l)
if ((STATUS > 0)); then
echo -e $MESSAGE_0 $STATUS $MESSAGE_1;
($SEARCH_CMD | xargs $GREP_CMD);
exit 1;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment