Skip to content

Instantly share code, notes, and snippets.

@duggan
Forked from hartfordfive/pre-commit
Last active Dec 19, 2016
Embed
What would you like to do?
Git pre-commit hook to validate JSON files
#!/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