Created
October 2, 2024 04:01
-
-
Save sir-pinecone/f7e94aa2940f75f4114a667a437d3e81 to your computer and use it in GitHub Desktop.
Look for nocheckin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Stops the commit if the changes include the word `nocheckin`. | |
if which tput >/dev/null 2>&1; then | |
ncolors=$(tput colors) | |
fi | |
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then | |
RED="$(tput setaf 1)" | |
GREEN="$(tput setaf 2)" | |
YELLOW="$(tput setaf 3)" | |
BLUE="$(tput setaf 4)" | |
MAGENTA="$(tput setaf 5)" | |
CYAN="$(tput setaf 6)" | |
BOLD="$(tput bold)" | |
NORMAL="$(tput sgr0)" | |
DIM="\e[2m" | |
else | |
RED="" | |
GREEN="" | |
YELLOW="" | |
BLUE="" | |
MAGENTA="" | |
CYAN="" | |
BOLD="" | |
NORMAL="" | |
fi | |
if [[ $DISABLE_NOCHECKIN -eq 1 ]]; then | |
printf "${BOLD}${YELLOW}'nocheckin' test disabled${NORMAL}\n\n" | |
exit 0 | |
fi | |
printf "${BOLD}${YELLOW}Looking for 'nocheckin'${NORMAL}\n\n" | |
change_set=$(git status -s | grep "^[MARCD]") | |
file_types_to_check="\(.jai\|.c\|.cpp\|.inl\|.inc\|.h\|.hpp\|.txt\|.md\|.js\|.py\|.ino\|.cs\|.cshtml\|.config\|.variables\|.gl\|.keymap\)$" | |
abort_commit=0 | |
while IFS= read -r line | |
do | |
# It's a lot faster to use parameter expansion rather than `cut`. | |
action="${line::1}" # Action is either one or two characters followed by N spaces, but we can work off the first character. | |
change="${line:2}" # Skip action and strip all leading spaces. | |
change="${change#"${change%%[![:space:]]*}"}" | |
#echo "action: '$action' | change: '$change'" | |
if [[ $action == "D" ]]; then | |
continue | |
elif [[ $action == "R" ]]; then | |
# Extract the new filename. | |
change=$(awk -F " -> " '{print $2}' <<< "$change") | |
fi | |
# Strip leading and trailing double quotes. This is faster than using sed like so: | |
# filename=$(sed -e 's/^"//' -e 's/"$//' <<< "$change") | |
filename="${change%\"}" | |
filename="${filename#\"}" | |
file_path="$(pwd)/${filename}" | |
#echo "file path: $file_path" | |
look_at_file=$(grep -e "$file_types_to_check" -i <<< "$filename") | |
if [[ "$look_at_file" != "" ]]; then | |
index=1 | |
printf "${MAGENTA}==> ${NORMAL}${BOLD}${filename}${NORMAL}\n" | |
errors=$(grep "nocheckin" "$file_path" >&1) | |
if [[ "$errors" != "" ]]; then | |
abort_commit=1 | |
printf "${BOLD}${RED}$errors${NORMAL}\n" | |
fi | |
else | |
printf "${MAGENTA}(IGNORE) ==> ${NORMAL}${DIM}$filename${NORMAL}\n" | |
fi | |
done <<< "$change_set" # Make the loop run in the main shell process so that we can modify the outside variables. | |
if [[ $abort_commit -eq 1 ]]; then | |
printf "\n${RED}${BOLD}Aborting!${NORMAL}\n" | |
exit 1 | |
else | |
printf "\n${GREEN}${BOLD}All good!${NORMAL}\n" | |
fi | |
printf "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment