Skip to content

Instantly share code, notes, and snippets.

@chefren
Forked from jeekl/gist:5083519
Last active November 23, 2018 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chefren/9a2711e86ef54b19b082f3892eafcda0 to your computer and use it in GitHub Desktop.
Save chefren/9a2711e86ef54b19b082f3892eafcda0 to your computer and use it in GitHub Desktop.
git pre-commit hook to validate json objects so you don't commit broken json.
#!/usr/bin/env bash
# Runs all .json files through pythons json lint tool before commiting,
# to make sure that you don't commit broken json objects.
git_dir=$(git rev-parse --show-toplevel)
for file in $(git diff-index --name-only --diff-filter=ACM --cached HEAD -- \
| grep -P '\.(json)$'); do
python -mjson.tool $file 2> /dev/null
if [ $? -ne 0 ] ; then
echo "Lint check of JSON object failed. Your changes were not commited."
echo "in $git_dir/$file:"
python -mjson.tool $file
exit 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment