Skip to content

honeyhive.utils.git_context

Git context collection for experiment metadata.

Collects git repository information (commit, branch, remote URL, etc.) from the current working directory to stamp on experiment run metadata.

logger module-attribute

logger = get_logger('honeyhive.utils.git_context')

get_git_context

get_git_context(
    cwd: Optional[str] = None,
) -> Dict[str, Any]

Collect git context from the current working directory.

Returns a dictionary with git metadata suitable for stamping on experiment run metadata. Returns an empty dict if not in a git repo or git is unavailable.

Parameters:

Name Type Description Default
cwd Optional[str]

Working directory to run git commands in. Defaults to the current process working directory.

None

Returns:

Type Description
Dict[str, Any]

Dictionary with git context fields: - commit_hash: Full SHA of HEAD commit - commit_hash_short: Short SHA of HEAD commit - branch: Current branch name - commit_message: Subject line of HEAD commit - author_name: Author name of HEAD commit - author_email: Author email of HEAD commit - remote_url: URL of the 'origin' remote - is_dirty: Whether the working tree has uncommitted changes

Source code in src/honeyhive/utils/git_context.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def get_git_context(cwd: Optional[str] = None) -> Dict[str, Any]:
    """Collect git context from the current working directory.

    Returns a dictionary with git metadata suitable for stamping on
    experiment run metadata. Returns an empty dict if not in a git repo
    or git is unavailable.

    Args:
        cwd: Working directory to run git commands in. Defaults to the
            current process working directory.

    Returns:
        Dictionary with git context fields:
            - commit_hash: Full SHA of HEAD commit
            - commit_hash_short: Short SHA of HEAD commit
            - branch: Current branch name
            - commit_message: Subject line of HEAD commit
            - author_name: Author name of HEAD commit
            - author_email: Author email of HEAD commit
            - remote_url: URL of the 'origin' remote
            - is_dirty: Whether the working tree has uncommitted changes
    """
    rev_parse = _run_git_command(["rev-parse", "--is-inside-work-tree"], cwd=cwd)
    if rev_parse != "true":
        return {}

    context: Dict[str, Any] = {}

    commit_hash = _run_git_command(["rev-parse", "HEAD"], cwd=cwd)
    if commit_hash:
        context["commit_hash"] = commit_hash

    commit_hash_short = _run_git_command(["rev-parse", "--short", "HEAD"], cwd=cwd)
    if commit_hash_short:
        context["commit_hash_short"] = commit_hash_short

    branch = _run_git_command(["rev-parse", "--abbrev-ref", "HEAD"], cwd=cwd)
    if branch:
        context["branch"] = branch

    commit_message = _run_git_command(["log", "-1", "--format=%s"], cwd=cwd)
    if commit_message:
        context["commit_message"] = commit_message

    author_name = _run_git_command(["log", "-1", "--format=%an"], cwd=cwd)
    if author_name:
        context["author_name"] = author_name

    author_email = _run_git_command(["log", "-1", "--format=%ae"], cwd=cwd)
    if author_email:
        context["author_email"] = author_email

    remote_url = _run_git_command(["remote", "get-url", "origin"], cwd=cwd)
    if remote_url:
        context["remote_url"] = remote_url

    status_output = _run_git_command(["status", "--porcelain"], cwd=cwd)
    if status_output is not None:
        context["is_dirty"] = len(status_output) > 0

    return context