Created
March 4, 2013 16:32
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
the grep part doesn't work to for me on macosx. Executing it will get me a grep usage
[develop][~/Desktop/repo/.git/hooks]$ ./pre-commit
usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
I wonder if the repository structure is expected to be in a certain way
@adv0r, macosx's grep provides "-E (extended grep)" option, try using grep -E '\.((js)|(json))$'
@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
Thanks for the script. I'm also using it to check if the json file is valid against sensu's conf: