File size: 927 Bytes
e1ced61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Best-effort read of the current repository commit for stage provenance.

Every stage manifest records the code commit that produced it (``docs/02`` §1).
Git is the source of truth but is never required for correctness: if it is
unavailable (e.g. a tarball install), we record ``<unknown>`` rather than fail
the pipeline.
"""

from __future__ import annotations

import subprocess

from .paths import repo_root


def current_code_commit() -> str:
    """Return the HEAD commit hash, or ``<unknown>`` if git is unavailable."""
    try:
        result = subprocess.run(
            ["git", "-C", str(repo_root()), "rev-parse", "HEAD"],
            capture_output=True,
            text=True,
            timeout=10.0,
            check=False,
        )
    except (FileNotFoundError, subprocess.TimeoutExpired):
        return "<unknown>"
    out = result.stdout.strip()
    return out.splitlines()[0] if out else "<unknown>"