import os

PROJECT_ROOT = os.path.dirname(os.path.realpath(os.path.join(__file__, '..')))


def get_git_rev(path=PROJECT_ROOT, search_parent_directories=True):
    try:
        import git
    except ImportError:
        print(
            "Warning: gitpython not installed."
            " Unable to log git rev."
            " Run `pip install gitpython` if you want git revs to be logged.")
        return None

    try:
        repo = git.Repo(
            path, search_parent_directories=search_parent_directories)
        git_rev = repo.active_branch.commit.name_rev
    except TypeError:
        git_rev = repo.head.object.name_rev
    except git.InvalidGitRepositoryError:
        return None

    return git_rev
