Skip to content

Instantly share code, notes, and snippets.

@duggan
Forked from hartfordfive/pre-commit
Last active December 19, 2016 15:32
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 duggan/9d4d27a63ec3fcf19544fe3d98698207 to your computer and use it in GitHub Desktop.
Save duggan/9d4d27a63ec3fcf19544fe3d98698207 to your computer and use it in GitHub Desktop.
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