Skip to content

The agent that grows with you

fix(update): launch-time pull repair runs the installer's git, not a bare `git`

Teknium101dd91f8f06
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.py
index 45a1455939c..479accd3583 100644
--- a/hermes_cli/_early_recovery.py
+++ b/hermes_cli/_early_recovery.py
@@ -244,6 +244,35 @@ def interrupted_pull_marker(root: Path) -> Path:
return _git_dir(root) / INTERRUPTED_PULL_MARKER
+def _git_executable() -> str:
+ """The git ``hermes update`` runs (``_subprocess_compat.expose_pm_git``), without installing it.
+
+ PATH's git first. A Windows install whose only git is the one install.ps1 staged in PM's store
+ has none on PATH until the updater exposes it, so this falls back to that copy: PM's recorded
+ entry, or the lockfile's pinned entry the installer extracted without recording it. A bare
+ ``git`` there dies with WinError 2 and the torn tree this repair exists for stays torn.
+ """
+ import shutil
+
+ found = shutil.which("git")
+ if found:
+ return found
+ with contextlib.suppress(Exception): # no PM, no store, no Windows git package: PATH's answer stands
+ import pm
+ from pm import paths
+ from pm.lock import Lockfile
+
+ recorded = pm.installed_package("git", allow_outdated=True)
+ if recorded is not None and recorded.binary is not None and recorded.binary.is_file():
+ return str(recorded.binary)
+ package, target = pm.get_package("git"), pm.current_target()
+ version = Lockfile(paths.lockfile_path()).version("git")
+ staged = package.binary(paths.store_root() / package.store_entry(version, target), target)
+ if staged is not None and staged.is_file():
+ return str(staged)
+ return "git"
+
+
def _trees_git_could_write(git, pre: str, target: str) -> tuple[list[str], set[str]]:
"""The trees the killed git was moving the checkout to, and the paths whose new content is unknowable.
@@ -315,9 +344,12 @@ def _paths_git_wrote(git, root: Path, pre: str, target: str) -> tuple[list[str],
written = True
else: # git's own file cut short starts one of the new blobs
content = file.read_bytes()
- written = any(subprocess.run(["git", "-C", str(root), "cat-file", "--filters", f"--path={path}", blob],
- capture_output=True, check=True, timeout=120,
- stdin=subprocess.DEVNULL).stdout.startswith(content) for blob in blobs)
+ written = False
+ for blob in blobs:
+ shown = git("cat-file", "--filters", f"--path={path}", blob, text=False)
+ if shown.returncode != 0:
+ raise subprocess.SubprocessError(shown.stderr.decode(errors="replace").strip())
+ written = written or shown.stdout.startswith(content)
if written:
(added if old_blob is None else restore).append(path)
new_dirs = {str(parent) for path, (_m, old_blob, _n) in entries.items() if old_blob is None
@@ -602,10 +634,12 @@ def _restore_holding_claim(root: Path, marker: Path, *, after_failure: bool = Fa
pre, target = fields.get("pre", "").strip(), fields.get("target", "").strip()
stash = fields.get("stash", "").strip()
- def git(*args: str, stdin: str | None = None) -> subprocess.CompletedProcess:
- return subprocess.run(["git", "--literal-pathspecs", "-C", str(root), *args], input=stdin,
- capture_output=True, text=True, encoding="utf-8", errors="replace",
- timeout=120, stdin=None if stdin is not None else subprocess.DEVNULL)
+ executable = _git_executable()
+
+ def git(*args: str, stdin: str | None = None, text: bool = True) -> subprocess.CompletedProcess:
+ return subprocess.run([executable, "--literal-pathspecs", "-C", str(root), *args], input=stdin,
+ capture_output=True, timeout=120, stdin=None if stdin is not None else subprocess.DEVNULL,
+ **({"text": True, "encoding": "utf-8", "errors": "replace"} if text else {}))
if not pre or not target or git("rev-parse", "HEAD").stdout.strip() != pre:
marker.unlink() # git finished (HEAD moved) or the marker is unusable
diff --git a/tests/hermes_cli/test_update_interrupted_pull.py b/tests/hermes_cli/test_update_interrupted_pull.py
index 0705d80a64b..25a89925d11 100644
--- a/tests/hermes_cli/test_update_interrupted_pull.py
+++ b/tests/hermes_cli/test_update_interrupted_pull.py
@@ -148,6 +148,42 @@ def test_killed_pull_is_restored_on_next_launch_and_update_reruns(checkout, monk
_pull(root) # `hermes update` again: a normal fast-forward
assert _git(root, "rev-parse", "HEAD") == b and not marker.exists()
+
+def test_restore_runs_the_installers_store_git_when_path_has_none(checkout, monkeypatch, tmp_path):
+ """Windows installs whose only git is the copy install.ps1 staged in PM's store: the launch-time
+ restore must find it like the updater does, release the dead git's index.lock and put the tree back
+ (a bare ``git`` died with WinError 2 there and every update for 10 minutes refused on the lock)."""
+ import shutil
+
+ import pm
+ import pm.paths
+
+ root, a, b = checkout
+ (root / "utils.py").write_text("NEW = 1\n", encoding="utf-8", newline="") # the killed ff's first write
+ (root / ".git" / "index.lock").write_bytes(b"") # left by the killed git
+ dead = subprocess.Popen([sys.executable, "-c", "pass"])
+ dead.wait()
+ er.interrupted_pull_marker(root).write_text(f"pid={dead.pid}\npre={a}\ntarget={b}\nstash=\n", encoding="utf-8")
+
+ real_git = shutil.which("git")
+ store = tmp_path / "tools"
+ version = pm.Lockfile(pm.paths.lockfile_path()).version("git")
+ staged = store / f"git-{version}-win32-x64" / "cmd" / "git.exe"
+ staged.parent.mkdir(parents=True)
+ calls = tmp_path / "staged-git-calls"
+ staged.write_text(f'#!/bin/sh\necho "$@" >> "{calls}"\nexec "{real_git}" "$@"\n', encoding="utf-8")
+ staged.chmod(0o755)
+ with monkeypatch.context() as windows:
+ windows.setattr(pm.paths, "store_root", lambda: store)
+ windows.setattr(pm, "current_target", lambda: "win32-x64")
+ windows.setenv("PATH", str(tmp_path / "no-git-here"))
+ assert er.restore_interrupted_pull(root) is True
+ assert calls.read_text(encoding="utf-8-sig").count("rev-parse HEAD") >= 1, "the store's git ran the restore"
+ assert _git(root, "rev-parse", "HEAD") == a and _git(root, "status", "--porcelain") == ""
+ assert not (root / ".git" / "index.lock").exists() and not er.interrupted_pull_marker(root).exists()
+ _pull(root) # the next `hermes update` is not refused on the lock
+ assert _git(root, "rev-parse", "HEAD") == b
+
# Every console script (`hermes`, `hermes-agent`, `hermes-acp`) repairs before its entry module imports
# any other checkout module past hermes_bootstrap: any of them may be a half-written file.
repo = os.path.realpath(Path(er.__file__).parent.parent)