from pathlib import Path
from typing import TypedDict


class GitMetadata(TypedDict, total=False):
    project: str
    branch: str
    commit: str


def get_git_metadata(root: Path) -> GitMetadata:
    try:
        import git

        repo = git.Repo(root, search_parent_directories=True)
        project = Path(repo.working_tree_dir or root).name
        branch = repo.active_branch.name if not repo.head.is_detached else "detached"
        commit = repo.head.commit.hexsha
        return {"project": project, "branch": branch, "commit": commit}
    except Exception:
        return {}
