import logging
import os

LOGGER = logging.getLogger(__name__)

class NoRepoFoundError(Exception):
    pass

def find_git_repo():
    location = os.path.abspath('.')
    while location != '/':
        check = os.path.join(location, '.git')
        if os.path.exists(check) and os.path.isdir(check):
            return check
        location = os.path.dirname(location)
    raise NoRepoFoundError("Could not find a git repository (.git) in {}".format(os.path.abspath('.')))

def install():
    repo = find_git_repo()
    LOGGER.debug("Found git repo at %s", repo)

import logging
import os

LOGGER = logging.getLogger(__name__)

class NoRepoFoundError(Exception):
    pass

class PreCommitExists(Exception):
    pass

def find_git_repo():
    location = os.path.abspath('.')
    while location != '/':
        check = os.path.join(location, '.git')
        if os.path.exists(check) and os.path.isdir(check):
            return check
        location = os.path.dirname(location)
    raise NoRepoFoundError("Could not find a git repository (.git) in {}".format(os.path.abspath('.')))

HOOK_CONTENT = """
mothermayi run
"""

def write_hook(pre_commit):
    with open(pre_commit, 'w') as f:
        f.write(HOOK_CONTENT)

def install():
    repo = find_git_repo()
    LOGGER.debug("Found git repo at %s", repo)
    hooks = os.path.join(repo, 'hooks')
    pre_commit = os.path.join(hooks, 'pre-commit')
    if os.path.exists(pre_commit):
        raise PreCommitExists("A git hook already exists at {}. Refusing to overwrite. Please remove it manually".format(pre_commit))
    write_hook(pre_commit)

Add initial pass at writing the git commit hook