Skip to content

The agent that grows with you

fix(update): expire fetch reflogs that pin stale shallow grafts

liuhao10246b51b0fd4e85
diff --git a/hermes_cli/gitlock.py b/hermes_cli/gitlock.py
index a7ea5f771aa..ec44a2620ea 100644
--- a/hermes_cli/gitlock.py
+++ b/hermes_cli/gitlock.py
@@ -349,10 +349,11 @@ def prune_stale_shallow_grafts(repo_root: Path) -> int:
break ``merge-base`` and push ``hermes update`` into the orphan-divergence reset path
on every run. Keep only the boundaries that still protect referenced tips (HEAD,
FETCH_HEAD, and every ref tip): the dropped commits are already unreachable and their
- objects are left for ``git gc``. Returns the number of graft lines removed; never
- raises, and restores the original file if the trimmed set breaks history walking
- (including the ``--reflog`` walk, so a graft a reflog-only commit still needs is
- never dropped, #108286).
+ objects are left for ``git gc``. Fetch reflogs naming a dropped graft are expired
+ first — otherwise they pin it and the prune rolls back forever (#124645). Returns
+ the number of graft lines removed; never raises, and restores the original file if
+ the trimmed set breaks history walking (including the ``--reflog`` walk, so a graft
+ a reflog-only commit still needs is never dropped, #108286).
"""
try:
shallow_path = _shallow_file_path(repo_root)
@@ -371,6 +372,22 @@ def prune_stale_shallow_grafts(repo_root: Path) -> int:
}
if len(keep) == len(lines):
return 0
+ # A dropped graft's parent was never fetched (depth-1), so a fetch reflog
+ # still naming it makes the fail-safe walk below fail and rolls the prune
+ # back on every run — grafts keep accumulating and the next update falls
+ # into orphan divergence (#124645). Fetch-history reflogs are the only safe
+ # ones to expire; reflogs users read (HEAD, local branches) keep their
+ # entries, and the fail-safe still rolls the prune back for those.
+ dropped = set(lines) - keep
+ for ref in _git_stdout_lines(
+ repo_root, ["for-each-ref", "--format=%(refname)", "refs/remotes/"]
+ ):
+ entries = _git_stdout_lines(repo_root, ["reflog", "show", "--format=%H", ref])
+ if set(entries) & dropped:
+ subprocess.run(
+ ["git", "reflog", "expire", "--expire=now", ref],
+ cwd=str(repo_root), capture_output=True, timeout=10,
+ )
_write_shallow(shallow_path, "\n".join(sorted(keep)) + "\n", suffix=".hermes-prune")
# Fail-safe: if any reachable walk now crosses a boundary we wrongly
# removed, put the grafts back — a growing file beats a broken repo.
diff --git a/tests/hermes_cli/test_shallow_boundary_repair.py b/tests/hermes_cli/test_shallow_boundary_repair.py
index eb5987f3d14..a9381f95231 100644
--- a/tests/hermes_cli/test_shallow_boundary_repair.py
+++ b/tests/hermes_cli/test_shallow_boundary_repair.py
@@ -177,6 +177,17 @@ def test_failed_shallow_maintenance_restores_original_bytes(tmp_path, caplog, op
def test_bom_shallow_prune_rolls_back_without_losing_bytes(tmp_path, caplog, bom):
clone = fixture(tmp_path)
shallow = clone / ".git/shallow"
+ # Pin the droppable graft in HEAD's reflog while the file still parses:
+ # user reflogs are never expired (#124645 only expires fetch reflogs), so
+ # the fail-safe must roll back. (Before #124645 the CRLF-only case rolled
+ # back only because the fetch reflog pinned the graft — the very
+ # accumulation that issue fixes.)
+ head_sha = git(clone, "rev-parse", "HEAD").stdout.strip()
+ tip_sha = git(clone, "rev-parse", "origin/main").stdout.strip()
+ middle_sha = next(line for line in shallow.read_text().splitlines()
+ if line and line not in (head_sha, tip_sha))
+ git(clone, "checkout", "-q", middle_sha)
+ git(clone, "checkout", "-q", "main")
original = bom + shallow.read_bytes().replace(b"\n", b"\r\n")
shallow.write_bytes(original)
with caplog.at_level("DEBUG", logger="hermes_cli.gitlock"):
diff --git a/tests/hermes_cli/test_shallow_graft_prune.py b/tests/hermes_cli/test_shallow_graft_prune.py
index c444711b71a..fc1fde55d9c 100644
--- a/tests/hermes_cli/test_shallow_graft_prune.py
+++ b/tests/hermes_cli/test_shallow_graft_prune.py
@@ -32,6 +32,35 @@ def _shallow_lines(repo: Path) -> list:
]
+def _mk_sparse_shallow_scenario(tmp_path: Path) -> Path:
+ """Depth-1 clone whose origin advances two commits per fetch (#124645).
+
+ Unlike :func:`_mk_shallow_scenario` the dropped graft's parent is never
+ fetched, so the only thing pinning it is the remote-tracking reflog the
+ fetch writes — the shape real installs hit when upstream moves several
+ commits between checks.
+ """
+ origin = tmp_path / "origin"
+ origin.mkdir()
+ _git(origin, "init", "-q", "-b", "main")
+ _git(origin, "config", "user.email", "[email protected]")
+ _git(origin, "config", "user.name", "t")
+ for i in range(3):
+ _git(origin, "commit", "--allow-empty", "-q", "-m", f"c{i}")
+ clone = tmp_path / "clone"
+ subprocess.run(
+ ["git", "clone", "-q", "--depth", "1", f"file://{origin}", str(clone)],
+ check=True,
+ capture_output=True,
+ text=True,
+ )
+ for i in (3, 5):
+ _git(origin, "commit", "--allow-empty", "-q", "-m", f"c{i}")
+ _git(origin, "commit", "--allow-empty", "-q", "-m", f"c{i + 1}")
+ _git(clone, "fetch", "-q", "--depth", "1", "origin", "main")
+ return clone
+
+
def _mk_shallow_scenario(tmp_path: Path) -> Path:
"""Depth-1 clone whose origin advanced twice: shallow carries 3 grafts."""
origin = tmp_path / "origin"
@@ -118,3 +147,50 @@ def test_update_check_prunes_and_reports_count(tmp_path, monkeypatch, capsys):
assert _git(clone, "rev-list", "--count", "origin/main") == "1"
assert "pruned 2 stale shallow graft(s)" in out
assert "Update available (behind origin/main)." in out
+
+
+def test_prune_expires_fetch_reflogs_that_pin_dropped_grafts(tmp_path):
+ """The fetch reflog must not pin a dropped graft forever (#124645).
+
+ The dropped graft's parent was never fetched, so the ``--reflog`` fail-safe
+ walk starts at the reflog's old fetch tips and fails, rolling the prune back
+ on every run — grafts keep accumulating and the next update falls into
+ orphan divergence. Expiring the pinning fetch reflog lets the prune stick.
+ """
+ clone = _mk_sparse_shallow_scenario(tmp_path)
+ assert len(_shallow_lines(clone)) == 3 # HEAD graft + two fetch tips
+
+ head_sha = _git(clone, "rev-parse", "HEAD")
+ tip_sha = _git(clone, "rev-parse", "origin/main")
+ middle_sha = (set(_shallow_lines(clone)) - {head_sha, tip_sha}).pop()
+ assert middle_sha in _git(clone, "reflog", "show", "--format=%H", "origin/main").split()
+
+ removed = prune_stale_shallow_grafts(clone)
+
+ assert removed == 1
+ assert set(_shallow_lines(clone)) == {head_sha, tip_sha}
+ # The fetch reflog no longer pins the dropped graft, while HEAD's own
+ # reflog survives untouched and every walk the fail-safe runs stays healthy.
+ assert middle_sha not in _git(clone, "reflog", "show", "--format=%H", "origin/main").split()
+ assert _git(clone, "reflog", "show", "--format=%H", "HEAD").split()
+ assert _git(clone, "rev-list", "--count", "HEAD") == "1"
+ assert _git(clone, "rev-list", "--count", "origin/main") == "1"
+ assert _git(clone, "rev-list", "--count", "--all", "--reflog") == "2"
+
+
+def test_prune_still_rolls_back_when_user_reflogs_pin_the_graft(tmp_path):
+ """A graft pinned by HEAD's reflog stays: user reflogs are never expired."""
+ clone = _mk_sparse_shallow_scenario(tmp_path)
+ head_sha = _git(clone, "rev-parse", "HEAD")
+ tip_sha = _git(clone, "rev-parse", "origin/main")
+ middle_sha = (set(_shallow_lines(clone)) - {head_sha, tip_sha}).pop()
+ _git(clone, "checkout", "-q", middle_sha) # a user checkout records it in HEAD's reflog
+ _git(clone, "checkout", "-q", "main")
+ head_reflog = _git(clone, "reflog", "show", "--format=%H", "HEAD").split()
+ assert middle_sha in head_reflog
+
+ removed = prune_stale_shallow_grafts(clone)
+
+ assert removed == 0 # the fail-safe still rolls the prune back
+ assert len(_shallow_lines(clone)) == 3
+ assert _git(clone, "reflog", "show", "--format=%H", "HEAD").split() == head_reflog