-
-
Save duggan/9d4d27a63ec3fcf19544fe3d98698207 to your computer and use it in GitHub Desktop.
Git pre-commit hook to validate JSON files
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
#!/bin/bash | |
# Comments: | |
# 1. You must add execution rights to this file (chmod u+x pre-commit) | |
# 2. You need to have the 'jq' package installed to parse the json | |
if [ "$(command -v jq )" == "" ] ; then | |
echo "jq required for pre-commit hook $0" | |
exit 1 | |
fi | |
if git-rev-parse --verify HEAD >/dev/null 2>&1; then | |
against=HEAD | |
else | |
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 | |
fi | |
bad_file=0 | |
for FILE in `git diff-index --name-status $against -- | cut -c3-` ; do | |
if [ ${FILE: -5} == ".json" ]; then | |
if [ -f ${FILE} ]; then | |
jq . $FILE >/dev/null | |
if [[ $? -ne 0 ]]; then | |
echo "JSON parsing failed for: $FILE" | |
bad_file=1 | |
fi | |
fi | |
fi | |
done | |
if [[ $bad_file -eq 1 ]]; then | |
exit 1 | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment