Created
February 14, 2018 03:02
-
-
Save Aupajo/0fdddf02163acb5fdd5de9dd0b45fdd0 to your computer and use it in GitHub Desktop.
Check for `.only` in a Git pre-commit hook. Gives you the option to show the diffs and continue with the commit.
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 | |
match_pattern=".only" | |
file_pattern="*.js" | |
# Exit if any command fails | |
set -e | |
function matching_diff { | |
git diff --staged -G "$match_pattern" $@ -- $file_pattern | |
} | |
if [ "$(matching_diff)" != "" ]; then | |
# Read from stdin to support reading prompts | |
exec < /dev/tty | |
echo "WARNING! The following files to be committed contain '$match_pattern':" | |
matching_diff --name-status | |
read -p "==> Show '$match_pattern' matches? [Y/n] " answer | |
if [ "$answer" != "n" ]; then | |
matching_diff | |
fi | |
read -p "==> Are you sure you want to continue with your commit? [y/N] " answer | |
if [ "$answer" != "y" ]; then | |
echo "Halting commit due to presence of '$match_pattern'." | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment