Stamp the commit into src/cymsim/COMMIT in git archives (export-subst), so that a copy without .git still records its commit in the cache metadata
3 changed files+10−2
.gitattributesadded+1−0View file
@@ -0,0 +1 @@
1+src/cymsim/COMMIT export-subst
src/cymsim/COMMITadded+1−0View file
@@ -0,0 +1 @@
1+$Format:%H$
src/cymsim/cache.pymodified+8−2View file
@@ -112,14 +112,20 @@ def device() -> str:
112112
113113
114114 def code_version() -> dict:
115- """The git commit of the cymsim checkout this package runs from, if it is one."""
115+ """The git commit of the cymsim checkout this package runs from, or, for a copy made with
116+ ``git archive``, the commit stamped into the COMMIT file (export-subst, see .gitattributes)."""
116117 root = Path(__file__).resolve().parents[2]
117118 try:
118119 git = lambda *a: subprocess.run(["git", "-C", str(root), *a], capture_output=True, text=True,
119120 timeout=10, check=True).stdout.strip()
120121 return {"commit": git("rev-parse", "HEAD"), "dirty": bool(git("status", "--porcelain", "--untracked-files=no"))}
121122 except (OSError, subprocess.SubprocessError):
122- return {"commit": None, "dirty": None}
123+ pass
124+ try:
125+ stamp = (Path(__file__).parent / "COMMIT").read_text().strip()
126+ except OSError:
127+ stamp = ""
128+ return {"commit": stamp, "dirty": None} if stamp and not stamp.startswith("$Format") else {"commit": None, "dirty": None}
123129
124130
125131 def _segment(n_from: int, n_to: int) -> dict: