Skip to content

The agent that grows with you

fix(update): keep the checkout lock in the git common dir

Tekniuma1998aedfba0
diff --git a/hermes_cli/update_lock.py b/hermes_cli/update_lock.py
index 7dcd4f98aa4..aa2cf1c80a2 100644
--- a/hermes_cli/update_lock.py
+++ b/hermes_cli/update_lock.py
@@ -11,10 +11,12 @@ Two artifacts, one authority each:
An owner is live while its pid is alive and its creation time still matches: never by age
(the 20-minute ceiling only ages out v1 markers, which carry no creation time).
-* The checkout lock ``<install root>/.hermes-update.lock`` — a kernel lock (flock / msvcrt)
- that ``hermes update`` holds for its whole process tree, so two updates of one checkout
- started from different homes exclude each other and a killed updater whose completion
- child still runs keeps the checkout locked until that child exits.
+* The checkout lock ``<git common dir>/hermes-update.lock`` (``<install root>/.hermes-update.lock``
+ for a ZIP install with no ``.git``) — a kernel lock (flock / msvcrt) that ``hermes update``
+ holds for its whole process tree, so two updates of one checkout started from different
+ homes exclude each other and a killed updater whose completion child still runs keeps the
+ checkout locked until that child exits. In the git dir it is never a worktree file: no
+ ``git status``/autostash sees it, whatever the checked-out tree's ``.gitignore`` says.
"""
from __future__ import annotations
@@ -39,6 +41,7 @@ CREATE_TIME_TOLERANCE_SECONDS = 2.0
MARKER_NAME = ".hermes-update-in-progress"
CHECKOUT_LOCK_NAME = ".hermes-update.lock"
+GIT_CHECKOUT_LOCK_NAME = "hermes-update.lock"
# Set by an orchestrating updater (Tauri `hermes-setup --update`) to its own pid before
# spawning `hermes update` as a child stage; the parent holds the marker for its whole run,
@@ -83,8 +86,35 @@ def _default_install_root() -> Path:
return Path(__file__).resolve().parents[1]
+def _git_common_dir(root: Path) -> Path | None:
+ """The repository's common git dir, read from disk (no git process: -I -S children).
+
+ ``.git`` is the dir itself, or a ``gitdir: <path>`` file (linked worktree, submodule) whose
+ target may name the shared dir in ``commondir``. ``None`` when ``root`` is no checkout.
+ """
+ dot = root / ".git"
+ try:
+ if dot.is_dir():
+ gitdir = dot
+ elif dot.is_file():
+ text = dot.read_text(encoding="utf-8-sig").strip()
+ if not text.startswith("gitdir:"):
+ return None
+ gitdir = root / text[len("gitdir:"):].strip() # an absolute target replaces root
+ else:
+ return None
+ common = gitdir / "commondir"
+ if common.is_file():
+ gitdir = gitdir / common.read_text(encoding="utf-8-sig").strip()
+ except OSError:
+ return None
+ return Path(os.path.normpath(gitdir))
+
+
def checkout_lock_path(install_root: Path | str | None = None) -> Path:
- return Path(install_root or _default_install_root()) / CHECKOUT_LOCK_NAME
+ root = Path(install_root or _default_install_root())
+ common = _git_common_dir(root)
+ return root / CHECKOUT_LOCK_NAME if common is None else common / GIT_CHECKOUT_LOCK_NAME
def _pid_alive(pid: int) -> bool:
diff --git a/tests/hermes_cli/test_update_lock.py b/tests/hermes_cli/test_update_lock.py
index fb39466ade7..50130dc6751 100644
--- a/tests/hermes_cli/test_update_lock.py
+++ b/tests/hermes_cli/test_update_lock.py
@@ -360,7 +360,35 @@ def test_checkout_lock_outlives_its_owner_while_an_inheriting_child_runs(tmp_pat
child.kill()
child.wait()
assert not update_in_progress(install)
- assert checkout_lock_path(install).name == ".hermes-update.lock"
+ assert checkout_lock_path(install).name == ".hermes-update.lock" # no .git: a ZIP install
+
+
+def test_git_checkout_lock_lives_in_the_common_git_dir_and_never_dirties_the_tree(tmp_path):
+ """A held lock must never show in `git status`: the updater's autostash would stash its own
+ lock file in any tree whose .gitignore lacks it (fixtures, forks, a branch switch)."""
+ install = tmp_path / "checkout"
+ env = {**os.environ, "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1"}
+
+ def git(*args, cwd=install):
+ return subprocess.run(["git", *args], cwd=cwd, env=env, check=True, capture_output=True,
+ text=True, encoding="utf-8").stdout
+
+ install.mkdir()
+ git("init", "-q")
+ git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "one")
+ linked = tmp_path / "linked"
+ git("worktree", "add", "-q", "--detach", str(linked))
+ common = Path(os.path.normpath(install / ".git"))
+ assert checkout_lock_path(install) == common / "hermes-update.lock"
+ assert checkout_lock_path(linked) == common / "hermes-update.lock"
+
+ lock = UpdateLock(path=tmp_path / "marker", install_root=install)
+ assert lock.acquire() is True
+ try:
+ assert git("status", "--porcelain", "--untracked-files=all") == ""
+ assert update_in_progress(linked), "a linked worktree shares the repository's lock"
+ finally:
+ lock.release()
def test_unwritable_install_root_refuses(tmp_path):