diff --git a/scripts/core_probe.py b/scripts/core_probe.py index 8f5c3bd0d..c02297acf 100644 --- a/scripts/core_probe.py +++ b/scripts/core_probe.py @@ -671,14 +671,20 @@ def host_identity() -> dict: } +def head_commit(root: Path = ROOT) -> str: + """The commit checked out at *root*. Raises OSError or CalledProcessError + when git cannot say: each caller decides what a record holds then.""" + return subprocess.run(["git", "rev-parse", "HEAD"], cwd=root, capture_output=True, + text=True, check=True).stdout.strip() + + def frontend_identity(script: Path = SCRIPT_PATH, label: str = "scripts/core_probe.py") -> dict: """The commit a measurement came from, and whether its script differs from it.""" commit: str | None = None dirty: bool | None = None try: - commit = subprocess.run(["git", "rev-parse", "HEAD"], cwd=ROOT, capture_output=True, - text=True, check=True).stdout.strip() + commit = head_commit() status = subprocess.run(["git", "status", "--porcelain", "--", str(script)], cwd=ROOT, capture_output=True, text=True, check=True).stdout dirty = bool(status.strip()) diff --git a/scripts/probes/packprobe.py b/scripts/probes/packprobe.py index 31e5783ca..8d69c444a 100644 --- a/scripts/probes/packprobe.py +++ b/scripts/probes/packprobe.py @@ -14,7 +14,7 @@ from dataclasses import dataclass from pathlib import Path from unattended import worktree -from core_probe import (EXIT_BUSY, PROBE_LOCK_ENV, PROBE_LOCK_HELD_ENV, VERDICTS, host_probe_lock, +from core_probe import (EXIT_BUSY, PROBE_LOCK_ENV, PROBE_LOCK_HELD_ENV, VERDICTS, head_commit, host_probe_lock, probe_lock_path) from core.paths import REPO_ROOT from core.statefile import now_iso @@ -392,9 +392,12 @@ def probe_one(layout: Layout, platform: str, config: dict, profile: dict, target def git_head() -> str | None: - proc = subprocess.run(["git", "rev-parse", "HEAD"], capture_output=True, text=True, check=False, - cwd=REPO_ROOT) - return proc.stdout.strip() or None + """The revision the pack was built from, None when git cannot say.""" + try: + return head_commit(REPO_ROOT) or None + except (OSError, subprocess.CalledProcessError) as exc: + print(f"WARNING: pack source commit unknown, git failed: {exc}", file=sys.stderr) + return None def append_line(line: dict, root: Path = REPO_ROOT) -> dict: diff --git a/tests/test_pack_probe.py b/tests/test_pack_probe.py index 1c58cfe08..f12d26ea6 100644 --- a/tests/test_pack_probe.py +++ b/tests/test_pack_probe.py @@ -226,6 +226,23 @@ class Line(unittest.TestCase): self.assertEqual(pp.main(["plan", "-p", "Lakka"]), 2) +class SourceCommit(unittest.TestCase): + """repo_commit reads HEAD through core_probe.head_commit, the reader + frontend_identity uses, and says when the checkout cannot answer.""" + + def test_the_checkout_head(self) -> None: + import core_probe + self.assertEqual(packprobe.git_head(), core_probe.head_commit(REPO_ROOT)) + + def test_a_failing_read_is_none_and_said(self) -> None: + from unittest import mock + err = io.StringIO() + with mock.patch.object(packprobe, "head_commit", side_effect=OSError("absent")), \ + contextlib.redirect_stderr(err): + self.assertIsNone(packprobe.git_head()) + self.assertIn("pack source commit unknown", err.getvalue()) + + class SitePages(unittest.TestCase): """A pack probe runs before a release: its records stay in measurements/ and reach no page."""