Two related failure modes after a crashed/interrupted fetch on a shallow clone (git clone --depth 1 installs): 1. STALE LOCK WEDGES EVERY FETCH. A killed fetch can leave .git/shallow.lock behind; every later 'git fetch' then fails with 'Unable to create .../shallow.lock: File exists'. 'hermes update --check' reported a hard fetch failure, and the passive banner check swallowed the exception and compared stale refs. Add hermes_cli.gitlock.clear_stale_git_locks(), a guarded sweep (age + git-process check so a live fetch is never yanked) wired into the check path, the apply path, and the banner's passive check. 2. SHALLOW TIP-SHA COMPARE FALSE-POSITIVES. On a shallow clone the check cannot count commits, so it compares tip SHAs. Local cherry-picks on top of the remote tip (e.g. re-applied local patches) make HEAD differ from origin/main even though HEAD already contains it — a false 'update available' banner. Add hermes_cli.gitlock.is_ancestor_of_head() and use 'git merge-base --is-ancestor' in the CLI check and banner paths before reporting an update. Mirror in the desktop (update-count.ts gains an isAncestor input; main.ts probes merge-base --is-ancestor). Tests: tests/test_gitlock.py (9) covering stale/young/no-lock/no-repo sweeps and ancestry true/false; update-count.test.ts +3 for the isAncestor path.
112 lines
3.8 KiB
Python
112 lines
3.8 KiB
Python
"""Tests for hermes_cli.gitlock — stale git lock recovery + ancestry probe.
|
|
|
|
These cover the two failure modes that produced the false "update available"
|
|
notification and the hard ``update --check`` failure after a crashed fetch on
|
|
a shallow clone:
|
|
|
|
1. A stale ``.git/shallow.lock`` makes every later ``git fetch`` fail with
|
|
"File exists" unless cleared.
|
|
2. On a shallow clone the update check compares tip SHAs, so local
|
|
cherry-picks on top of the remote tip look like "update available" even
|
|
though HEAD already contains the remote tip.
|
|
|
|
The module's safety rules are also pinned: a *young* lock or a *running git
|
|
process* must never be cleared.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import subprocess
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from hermes_cli.gitlock import (
|
|
LOCK_NAMES,
|
|
STALE_LOCK_MIN_AGE_SECONDS,
|
|
clear_stale_git_locks,
|
|
is_ancestor_of_head,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def repo(tmp_path: Path) -> Path:
|
|
"""A real, tiny git repo with two commits (no network)."""
|
|
root = tmp_path / "repo"
|
|
root.mkdir()
|
|
subprocess.run(["git", "init", "-q"], cwd=root, check=True)
|
|
subprocess.run(["git", "config", "user.email", "test@example.com"], cwd=root, check=True)
|
|
subprocess.run(["git", "config", "user.name", "Test"], cwd=root, check=True)
|
|
(root / "a.txt").write_text("one\n")
|
|
subprocess.run(["git", "add", "a.txt"], cwd=root, check=True)
|
|
subprocess.run(["git", "commit", "-q", "-m", "first"], cwd=root, check=True)
|
|
(root / "b.txt").write_text("two\n")
|
|
subprocess.run(["git", "add", "b.txt"], cwd=root, check=True)
|
|
subprocess.run(["git", "commit", "-q", "-m", "second"], cwd=root, check=True)
|
|
return root
|
|
|
|
|
|
def _touch(path: Path, age_seconds: float) -> None:
|
|
"""Create (or truncate) a file and backdate its mtime."""
|
|
path.touch()
|
|
old = time.time() - age_seconds
|
|
os.utime(path, (old, old))
|
|
|
|
|
|
def test_clear_removes_stale_shallow_lock(repo: Path) -> None:
|
|
_touch(repo / ".git" / "shallow.lock", STALE_LOCK_MIN_AGE_SECONDS + 60)
|
|
removed = clear_stale_git_locks(repo)
|
|
assert str(repo / ".git" / "shallow.lock") in removed
|
|
assert not (repo / ".git" / "shallow.lock").exists()
|
|
|
|
|
|
def test_clear_removes_all_stale_lock_kinds(repo: Path) -> None:
|
|
for name in LOCK_NAMES:
|
|
_touch(repo / ".git" / name, STALE_LOCK_MIN_AGE_SECONDS + 60)
|
|
removed = clear_stale_git_locks(repo)
|
|
assert len(removed) == len(LOCK_NAMES)
|
|
for name in LOCK_NAMES:
|
|
assert not (repo / ".git" / name).exists()
|
|
|
|
|
|
def test_clear_keeps_young_lock(repo: Path) -> None:
|
|
_touch(repo / ".git" / "shallow.lock", 1) # 1 second old — presumably live
|
|
removed = clear_stale_git_locks(repo)
|
|
assert removed == []
|
|
assert (repo / ".git" / "shallow.lock").exists()
|
|
|
|
|
|
def test_clear_noop_on_non_repo(tmp_path: Path) -> None:
|
|
bare = tmp_path / "not-a-repo"
|
|
bare.mkdir()
|
|
assert clear_stale_git_locks(bare) == []
|
|
|
|
|
|
def test_clear_noop_with_no_locks(repo: Path) -> None:
|
|
assert clear_stale_git_locks(repo) == []
|
|
|
|
|
|
def test_is_ancestor_true_for_first_commit(repo: Path) -> None:
|
|
first = subprocess.run(
|
|
["git", "rev-list", "--max-parents=0", "HEAD"],
|
|
cwd=repo, capture_output=True, text=True, check=True,
|
|
).stdout.strip()
|
|
assert is_ancestor_of_head(repo, first) is True
|
|
|
|
|
|
def test_is_ancestor_true_for_head_itself(repo: Path) -> None:
|
|
head = subprocess.run(
|
|
["git", "rev-parse", "HEAD"], cwd=repo, capture_output=True, text=True, check=True,
|
|
).stdout.strip()
|
|
assert is_ancestor_of_head(repo, head) is True
|
|
|
|
|
|
def test_is_ancestor_false_for_unknown_rev(repo: Path) -> None:
|
|
assert is_ancestor_of_head(repo, "deadbeef" * 5) is False
|
|
|
|
|
|
def test_is_ancestor_false_for_nonexistent_repo(tmp_path: Path) -> None:
|
|
assert is_ancestor_of_head(tmp_path / "missing", "HEAD") is False
|