Skip to content

Instantly share code, notes, and snippets.

@graphaelli
Created March 2, 2015 14:49
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 graphaelli/34a475bb028a8e551192 to your computer and use it in GitHub Desktop.
Save graphaelli/34a475bb028a8e551192 to your computer and use it in GitHub Desktop.
pre-commit hook
#!/usr/bin/env python
import json
import subprocess
import sys
def against():
try:
subprocess.check_call("git rev-parse --verify HEAD".split())
return "HEAD"
except:
return "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
def check_json(filename):
with open(filename, mode="r") as jf:
try:
json.load(jf)
except Exception, e:
print "{} failed verification:\n{}".format(filename, e)
sys.exit(1)
def check_whitespace(version=None):
""" inspired by https://github.com/mozilla/moz-git-tools """
if not version:
version = against()
try:
subprocess.check_call("git diff-index --check --cached {}".format(version).split())
except subprocess.CalledProcessError:
print "whitespace verification failed"
sys.exit(1)
def staged_files():
for filename in subprocess.check_output("git diff --name-only --staged --diff-filter=AM".split()).split():
yield filename.strip()
def verify(filelist):
for json_file in filter(lambda x: x.endswith(".json"), filelist):
check_json(json_file)
if __name__ == '__main__':
verify(staged_files())
check_whitespace()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment