Skip to content

Instantly share code, notes, and snippets.

@Aupajo
Created February 14, 2018 03:02
Show Gist options
  • Save Aupajo/0fdddf02163acb5fdd5de9dd0b45fdd0 to your computer and use it in GitHub Desktop.
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.
#!/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