Skip to content

Instantly share code, notes, and snippets.

@neodon
Created July 5, 2022 20:26
Show Gist options
  • Save neodon/ff35afab6d3e973544371c9d78d72909 to your computer and use it in GitHub Desktop.
Save neodon/ff35afab6d3e973544371c9d78d72909 to your computer and use it in GitHub Desktop.
Pre-commit hook referenced by Reddit post
#!/bin/bash
# .git/hooks/pre-commit
# https://www.reddit.com/r/git/comments/3o1tut/precommit_hook_to_make_sure_you_updated_the/
set -o nounset
REPO_ROOT=$(git rev-parse --show-toplevel)
echo "Git repo is at $REPO_ROOT"
SITE_CHANGES=$(git status -s $REPO_ROOT | wc -l)
echo "Detected $SITE_CHANGES changes"
if [ "$SITE_CHANGES" -gt "0" ]; then
echo "Checking to make sure package version was updated..."
VERSION_CHANGED=$(git diff --cached -G '"version":' -- $REPO_ROOT/package.json | wc -l)
if [ "$VERSION_CHANGED" -gt "0" ]; then
echo "Version was updated! Continuing..."
else
echo "Version was not updated :( Aborting commit."
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment