refactor: share the probe commit reader

packprobe.git_head reads HEAD through core_probe.head_commit, the reader frontend_identity uses, and warns when git cannot answer instead of recording None silently.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 10:30:51 +01:00
1 parent 872ffc7cc6
commit 6f510be49a
3 files changed
+32 -6

No files matched your search

+8 -2
View File
@@ -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())
+7 -4
View File
@@ -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:
+17
View File
@@ -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."""