Skip to content

The agent that grows with you

fix(git): run the worktree reclaim ls-remote, unshallow fetch and prune under noninteractive_git_env

John Paul Soliva13ea517514fd
diff --git a/hermes_cli/worktree_ops.py b/hermes_cli/worktree_ops.py
index 45e3dc7f147..ba4321b5b9f 100644
--- a/hermes_cli/worktree_ops.py
+++ b/hermes_cli/worktree_ops.py
@@ -19,7 +19,7 @@ import uuid
from pathlib import Path
from typing import Dict, Optional
-from hermes_cli._subprocess_compat import kill_process_tree
+from hermes_cli._subprocess_compat import kill_process_tree, noninteractive_git_env
from hermes_constants import get_hermes_home
from utils import atomic_json_write
@@ -107,7 +107,8 @@ def _cleanup_failed_worktree_add(repo_root: str, wt_path: Path, branch_name: str
if wt_path.exists():
shutil.rmtree(wt_path, ignore_errors=True)
# `remove` needs the dir; `prune` drops the admin entry when it is already gone.
- _git_quiet(["worktree", "prune"], repo_root, timeout=15)
+ _git_quiet(["worktree", "prune"], repo_root, timeout=15,
+ stdin=subprocess.DEVNULL, env=noninteractive_git_env())
_git_quiet(["branch", "-D", branch_name], repo_root, timeout=15)
except Exception as e:
logger.debug("cleanup after failed worktree add: %s", e)
@@ -200,7 +201,8 @@ def _maintain_pack_health(repo_root: str) -> None:
logger.info("git pack sprawl (%d packs) — repacking in background", packs)
_run_bounded_repack(repo_root)
# Repacking can strand now-duplicated admin files; prune on the same pass.
- _git(["worktree", "prune"], repo_root, timeout=60, check=False)
+ _git(["worktree", "prune"], repo_root, timeout=60, check=False,
+ stdin=subprocess.DEVNULL, env=noninteractive_git_env())
except Exception as e:
logger.debug("pack maintenance skipped: %s", e)
@@ -601,7 +603,9 @@ def _deepen_shallow_repo(repo_root: str, timeout: int = 600) -> bool:
try:
for extra in (["--filter=blob:none"], []):
try:
- result = _git(["fetch", remote, "--unshallow", *extra], repo_root, timeout=timeout)
+ # Unattended fetch: a repo-level core.sshCommand / credential.helper must not run.
+ result = _git(["fetch", remote, "--unshallow", *extra], repo_root, timeout=timeout,
+ stdin=subprocess.DEVNULL, env=noninteractive_git_env())
except subprocess.TimeoutExpired:
return False
if result.returncode == 0:
@@ -750,7 +754,9 @@ def _fetch_remote_branch_heads(repo_root: str, timeout: int = 20) -> Optional[Di
remote-tracking ref and would read as unpushed forever.
"""
try:
- result = _git(["ls-remote", "--heads", "origin"], repo_root, timeout=timeout)
+ # Runs from the reclaim sweep, unattended: same transport pins as the base fetch.
+ result = _git(["ls-remote", "--heads", "origin"], repo_root, timeout=timeout,
+ stdin=subprocess.DEVNULL, env=noninteractive_git_env())
if result.returncode != 0:
return None
pairs = (line.split("\t", 1) for line in result.stdout.splitlines())
diff --git a/tests/security/test_gitspawn_config_injection.py b/tests/security/test_gitspawn_config_injection.py
index 4f8cd907928..0689e8de78e 100644
--- a/tests/security/test_gitspawn_config_injection.py
+++ b/tests/security/test_gitspawn_config_injection.py
@@ -251,6 +251,30 @@ def test_repo_named_filters_never_run_from_kanban_gc_or_hints(tmp_path):
assert sorted(p.name for p in tmp_path.glob("FILTER.*")) == []
+def test_worktree_reclaim_transport_calls_ignore_repo_ssh_command(tmp_path, monkeypatch):
+ """The reclaim sweep's ``ls-remote`` and the shallow-repo ``fetch --unshallow`` run unattended;
+ a repository-level ``core.sshCommand`` must not run on either."""
+ from hermes_cli import worktree_ops
+ clean = {**os.environ, "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_SYSTEM": os.devnull, "GIT_CONFIG_NOSYSTEM": "1"}
+ clean.pop("GIT_SSH_COMMAND", None)
+ src, repo, marker = tmp_path / "src", tmp_path / "repo", (tmp_path / "SSH").as_posix()
+ subprocess.run(["git", "init", "-q", str(src)], check=True, env=clean)
+ ident = ["-c", "user.email=a@b", "-c", "user.name=a"]
+ for n in ("1", "2"): # two commits, so a depth-1 clone is shallow
+ (src / "f").write_text(n)
+ subprocess.run(["git", "-C", str(src), "add", "f"], check=True, env=clean)
+ subprocess.run(["git", "-C", str(src), *ident, "commit", "-qm", n], check=True, env=clean)
+ subprocess.run(["git", "clone", "-q", "--depth", "1", src.as_uri(), str(repo)], check=True, env=clean)
+ for key, value in {"remote.origin.url": "ssh://[email protected]/x.git",
+ "core.sshCommand": f"touch '{marker}'; false"}.items():
+ subprocess.run(["git", "-C", str(repo), "config", key, value], check=True, env=clean)
+
+ monkeypatch.delenv("GIT_SSH_COMMAND", raising=False)
+ worktree_ops._fetch_remote_branch_heads(str(repo), timeout=20)
+ worktree_ops._deepen_shallow_repo(str(repo), timeout=20)
+ assert not Path(marker).exists()
+
+
def test_noninteractive_env_pins_fsmonitor_and_hooks():
env = noninteractive_git_env({})
values = {