Created
March 2, 2015 14:49
-
-
Save graphaelli/34a475bb028a8e551192 to your computer and use it in GitHub Desktop.
pre-commit hook
This file contains hidden or 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 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