#!/usr/bin/env python3

"""
A git hook to automatically run diffenv after a successful git commit.
Saves to file named for the git hash
"""

from subprocess import check_output, check_call, CalledProcessError
import sys
import os
from os.path import join

try:
    commit_hash = check_output(
        ['git', 'rev-parse', 'HEAD']).decode("utf-8").strip()
except CalledProcessError as e:
    sys.stderr.write(
        "ERROR: diffenv git hook could get git hash. Error: %e\n" %
        e)
    exit(1)

try:
    # Ensure our .diffenv/commits dir exists
    commits_dir = join(
        os.getcwd(), '..', '..', '.diffenv', 'commits')
    os.makedirs(commits_dir, exist_ok=True)
except e:
    sys.stderr.write(
        "ERROR: Could not create .diffenv/commits dir. Details: %s\n" %
        e)

try:
    # Note: GIT_DIR is set by git before hook runs
    diffenv_filename = join(commits_dir, commit_hash + '.diffenv')
    commit_hash = check_call(['diffenv', '-o', diffenv_filename])
except FileNotFoundError:
    sys.stderr.write("ERROR: diffenv hook ran, but diffenv isn't installed.\n")
    exit(1)
except CalledProcessError as e:
    sys.stderr.write("ERROR: Could not run diffenv git hook. Error: %e\n" % e)
    exit(1)
