Last active
December 13, 2018 21:04
-
-
Save MaffooBristol/6465538 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/bash | |
# IMPORTANT! | |
# On Ubuntu, /bin/sh does not point to Bash, but instead to "Dash", a highly | |
# stripped down version that doesn't allow certain more complex behaviours | |
# such as the brace expansion that you will see both in the DEBUG_GREP regex | |
# and in the -exec subroutine in FUSE_FIND. | |
# Allow input from the keyboard. | |
exec < /dev/tty | |
MODULE_PATH="/company/projects/company/www/sites/all/modules/custom/" | |
# Find all .fuse_hidden files in the module path. | |
FUSE_FIND=$(find $MODULE_PATH -name ".fuse_hidden*") | |
# If there are results, prompt the user for whether the files should be automatically deleted. | |
if [[ ${#FUSE_FIND} > 0 ]]; then | |
echo -e "\n > \033[1mGit Police Warning!\033[0m Odd .fuse_hidden files were found:\n" | |
echo -e "\033[1m\033[31m$FUSE_FIND\033[0m" | sed 's/\/company\/projects\/company\/www\// - /' | |
echo | |
read -e -p " > Would you like me to delete them? [Y/n] " -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
# Run the find command again, but exec "rm -rf" on them. | |
find $MODULE_PATH -name ".fuse_hidden*" -exec rm -rf {} \; | |
echo -e " > Successfully deleted .fuse_hidden files." | |
else | |
echo -e " > Ignored the existence of .fuse_hidden files." | |
fi | |
fi | |
# Find all diff/merge detritus. | |
MERGE_FIND=$(find $MODULE_PATH -regextype awk -regex ".*\.(orig|local|remote)") | |
# If there are results, prompt the user for whether the files should be automatically deleted. | |
if [[ ${MERGE_FIND} > 0 ]]; then | |
echo -e "\n > \033[1mGit Police Warning!\033[0m Mergetool backup detritus files were found:\n" | |
echo -e "\033[1m\033[31m$MERGE_FIND\033[0m" | sed 's/\/company\/projects\/company\/www\// - /' | |
echo | |
read -e -p " > Would you like me to delete them? [Y/n] " -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
# Run the find command again, but exec "rm -rf" on them. | |
find $MODULE_PATH -regextype awk -regex ".*\.(orig|local|remote)" -exec rm -rf {} \; | |
echo -e " > Successfully deleted mergetool files." | |
else | |
echo -e " > Ignored the existence of mergetool files." | |
fi | |
fi | |
# Experimental: looking into checking only staged files... | |
# CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACM) | |
# Grep for dpm, dsm and console.log(). Ignores commented lines, but doesn't work with multi-line comments yet (false positives). | |
DEBUG_GREP=$(grep -IiRn --exclude-dir={node_modules,vendor,lib,contrib,components,bower_components,dist,.tmp,.tmp-dist} --exclude={app,vendor}.js --include=\*.{module,inc,install,php,js} -P '^(?!.*\/\/).*(dpm\(|dsm\(|console.log\(|dkm\()' $MODULE_PATH) | |
# Check that there are results or exit 0 (continue operations). | |
# Then print the info and prompt the user, exiting 1 if they cancel and exiting 0 otherwise. | |
if [[ ${#DEBUG_GREP} > 0 ]]; then | |
echo -e "\n > \033[1mGit Police Warning!\033[0m There are illegal debugging lines in the following files:\n" | |
echo -e "$DEBUG_GREP" | sed 's/^\/company\/projects\/company\/www\// - /' | awk -F: '{print "\033[1m"$1" \033[34m+"$2": \033[31m"$3"\033[0m"}' | |
echo | |
read -e -p " > Do you wish to continue? [y/N] " -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
echo -e " > Okey dokey, but I did warn you!" | |
exit 0 | |
else | |
echo -e " > Cancelled the commit." | |
exit 1 | |
fi | |
else | |
exit 0 | |
fi | |
# Nothing should go wrong, but if so, it's probably best to abort the script! | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment