Skip to content

The agent that grows with you

fix(update): a shallow or offline checkout ahead of the release is never moved back to it

Tekniumdce1e9b37581
diff --git a/hermes_cli/source_releases.py b/hermes_cli/source_releases.py
index 8240560b56d..df260886851 100644
--- a/hermes_cli/source_releases.py
+++ b/hermes_cli/source_releases.py
@@ -95,7 +95,11 @@ def resolve_source_target(channel: str, git_cmd=None, cwd=None, *, repository=No
def _head_containing(git_cmd, cwd, commit: str, repository: str) -> str | None:
- """HEAD's sha when it equals or descends from ``commit``, else None."""
+ """HEAD's sha unless HEAD is PROVEN not to contain ``commit``, then None.
+
+ An unchosen default must never move a checkout backward on a guess, so an
+ unknown relation (shallow history, GitHub unreachable) keeps HEAD.
+ """
from hermes_cli.source_check import _github_compare, source_git_env
def run(*args):
@@ -107,11 +111,13 @@ def _head_containing(git_cmd, cwd, commit: str, repository: str) -> str | None:
if not _SHA.fullmatch(head):
return None
ancestry = run("merge-base", "--is-ancestor", commit, head).returncode
- if ancestry in (0, 1):
- return head if ancestry == 0 else None
- # A shallow checkout may lack the release commit; GitHub knows the relation.
+ if ancestry == 0:
+ return head
+ # A shallow boundary makes exit 1 a guess; full history makes it proof.
+ if ancestry == 1 and run("rev-parse", "--is-shallow-repository").stdout.strip() == "false":
+ return None
status = (_github_compare(commit, head, repository) or {}).get("status")
- return head if status in ("ahead", "identical") else None
+ return None if status in ("behind", "diverged") else head
def _resolve_source_target(channel: str, git_cmd=None, cwd=None, *, repository=None) -> SourceTarget:
diff --git a/tests/hermes_cli/test_stable_forward_only_shallow.py b/tests/hermes_cli/test_stable_forward_only_shallow.py
new file mode 100644
index 00000000000..e42a905ce62
--- /dev/null
+++ b/tests/hermes_cli/test_stable_forward_only_shallow.py
@@ -0,0 +1,51 @@
+"""An unchosen default channel never moves a checkout backward on a guess."""
+import subprocess
+
+import pytest
+
+from hermes_cli import source_check, source_releases
+
+
+def _git(cwd, *args):
+ return subprocess.run(["git", *args], cwd=cwd, check=True, capture_output=True, text=True).stdout.strip()
+
+
+def history(tmp_path):
+ origin = tmp_path / "origin"
+ origin.mkdir()
+ _git(origin, "init", "-q", "-b", "main")
+ shas = []
+ for n in range(3):
+ (origin / "f").write_text(str(n))
+ _git(origin, "add", "f")
+ _git(origin, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "-m", str(n))
+ shas.append(_git(origin, "rev-parse", "HEAD"))
+ return origin, shas # shas[0] is the "release", HEAD (shas[2]) is ahead of it
+
+
[email protected]("release_fetched", [False, True])
[email protected]("compare", [None, {"status": "ahead"}])
+def test_shallow_checkout_ahead_of_release_stays_put(history, tmp_path, monkeypatch, compare, release_fetched):
+ origin, shas = history
+ clone = tmp_path / "shallow"
+ _git(tmp_path, "clone", "-q", "--depth", "1", f"file://{origin}", str(clone))
+ if release_fetched: # present but cut off by the shallow boundary: is-ancestor exits 1
+ _git(origin, "tag", "v0.0.1", shas[0])
+ _git(clone, "fetch", "-q", "--depth", "1", "origin", "tag", "v0.0.1")
+ monkeypatch.setattr(source_check, "_github_compare", lambda *a, **k: compare)
+ # Shallow history hides the release commit: unknown (or proven ahead) keeps HEAD.
+ assert source_releases._head_containing(["git"], clone, shas[0], "o/r") == shas[2]
+ # GitHub proving HEAD is behind the release is the only case that moves it.
+ monkeypatch.setattr(source_check, "_github_compare", lambda *a, **k: {"status": "behind"})
+ assert source_releases._head_containing(["git"], clone, shas[0], "o/r") is None
+
+
+def test_full_history_behind_release_is_proof(history, tmp_path, monkeypatch):
+ origin, shas = history
+ clone = tmp_path / "full"
+ _git(tmp_path, "clone", "-q", str(origin), str(clone))
+ _git(clone, "checkout", "-q", shas[0])
+ monkeypatch.setattr(source_check, "_github_compare", lambda *a, **k: pytest.fail("no network on proof"))
+ assert source_releases._head_containing(["git"], clone, shas[2], "o/r") is None
+ assert source_releases._head_containing(["git"], clone, shas[0], "o/r") == shas[0]