Skip to content

Instantly share code, notes, and snippets.

@Aupajo Aupajo/pre-commit
Created Feb 14, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.