import pathlib
from typing import Optional


def find_git_root(fn: pathlib.Path) -> Optional[pathlib.Path]:
    """Given a file, find the git repository root (if it exists)."""
    while fn:
        if (fn / ".git").exists():
            return fn
        fn = fn.parent