Skip to content

Instantly share code, notes, and snippets.

@jeekl
Created March 4, 2013 16:32
Show Gist options
  • Save jeekl/5083519 to your computer and use it in GitHub Desktop.
Save jeekl/5083519 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 or .js 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 '\.((js)|(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
@dataf3l
Copy link

dataf3l commented Jul 12, 2019

@scheler "Does this work on JavaScript files too for you" -> you can use g instead of git, maybe?
https://github.com/dataf3l/g

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment