Skip to content

The agent that grows with you

fix(update): the fork sync counts and merges one resolved upstream commit (m2)

Tekniumf03358537f55
diff --git a/hermes_cli/update_cmd_git.py b/hermes_cli/update_cmd_git.py
index d8295ed3a1a..dddc07304c6 100644
--- a/hermes_cli/update_cmd_git.py
+++ b/hermes_cli/update_cmd_git.py
@@ -385,8 +385,11 @@ def _sync_with_upstream_if_needed(git_cmd: list[str], cwd: Path, *, assume_yes:
except subprocess.CalledProcessError:
print(" ✗ Failed to fetch upstream. Skipping upstream sync.")
return False
- origin_ahead = _count_commits_between(git_cmd, cwd, "upstream/main", "origin/main")
- upstream_ahead = _count_commits_between(git_cmd, cwd, "origin/main", "upstream/main")
+ # One commit for the whole sync (m2): the short `upstream/main` also names a local branch of
+ # that name, so the count and the merge could each see a different commit.
+ upstream = _git_stdout(git_cmd, ["rev-parse", "-q", "--verify", "refs/remotes/upstream/main^{commit}"], cwd)
+ origin_ahead = _count_commits_between(git_cmd, cwd, upstream, "refs/remotes/origin/main") if upstream else -1
+ upstream_ahead = _count_commits_between(git_cmd, cwd, "refs/remotes/origin/main", upstream) if upstream else -1
if origin_ahead < 0 or upstream_ahead < 0:
print(" ✗ Could not compare branches. Skipping upstream sync.")
return False
@@ -403,9 +406,9 @@ def _sync_with_upstream_if_needed(git_cmd: list[str], cwd: Path, *, assume_yes:
print(f"\n→ Fork is {upstream_ahead} commit(s) behind upstream\n→ Pulling from upstream...")
try:
# The fetch above already brought upstream/main: a local fast-forward (no network, so no
- # credential helper is started under the checkout lock fd a mutator inherits).
- run_git(git_cmd, ["merge", "--ff-only", "refs/remotes/upstream/main"], cwd=cwd, check=True,
- **_no_prompt_git_kwargs())
+ # credential helper is started under the checkout lock fd a mutator inherits) to the
+ # very commit counted above.
+ run_git(git_cmd, ["merge", "--ff-only", upstream], cwd=cwd, check=True, **_no_prompt_git_kwargs())
except subprocess.CalledProcessError:
print(" ✗ Failed to pull from upstream. You may need to resolve conflicts manually.")
return False
diff --git a/tests/hermes_cli/test_update_fetch_failure_classifier.py b/tests/hermes_cli/test_update_fetch_failure_classifier.py
index d1ca74d9f3f..994d17b1541 100644
--- a/tests/hermes_cli/test_update_fetch_failure_classifier.py
+++ b/tests/hermes_cli/test_update_fetch_failure_classifier.py
@@ -135,13 +135,14 @@ def test_update_and_upstream_network_calls_disable_terminal_prompts(monkeypatch,
monkeypatch.setenv("GIT_CONFIG_KEY_0", "credential.helper")
monkeypatch.setenv("GIT_CONFIG_VALUE_0", "fixture-helper")
monkeypatch.setattr(update_cmd, "_has_upstream_remote", lambda *a: True)
+ upstream = "c" * 40
monkeypatch.setattr(update_cmd, "_count_commits_between",
- lambda git, cwd, base, head: 2 if head == "upstream/main" else 0)
+ lambda git, cwd, base, head: 2 if head == upstream else 0)
calls = []
def run(cmd, **kwargs):
calls.append((cmd[1:], kwargs))
- return subprocess.CompletedProcess(cmd, 0, "", "")
+ return subprocess.CompletedProcess(cmd, 0, upstream if "rev-parse" in cmd else "", "")
monkeypatch.setattr(subprocess, "run", run)
update_cmd._git_run(["git"], ["fetch", "origin", "main"], cwd=tmp_path, network=True, check=True)
@@ -150,8 +151,10 @@ def test_update_and_upstream_network_calls_disable_terminal_prompts(monkeypatch,
# The fork's fast-forward is local (`merge --ff-only upstream/main` after the fetch): no
# credential helper ever runs under the checkout lock fd a mutator inherits (R2).
- assert [git_subcommand(args) for args, _ in calls] == ["fetch", "fetch", "merge", "push"]
- for args, kwargs in calls:
+ network = [(args, kwargs) for args, kwargs in calls if git_subcommand(args) != "rev-parse"]
+ assert [git_subcommand(args) for args, _ in network] == ["fetch", "fetch", "merge", "push"]
+ assert network[2][0][-1] == upstream # the counted commit, not a name a local branch can shadow
+ for args, kwargs in network:
assert kwargs["stdin"] is subprocess.DEVNULL, args
env = kwargs["env"]
assert env["GIT_TERMINAL_PROMPT"] == "0", args
diff --git a/tests/hermes_cli/test_update_fork_sync_identity.py b/tests/hermes_cli/test_update_fork_sync_identity.py
new file mode 100644
index 00000000000..fcb6c1418e0
--- /dev/null
+++ b/tests/hermes_cli/test_update_fork_sync_identity.py
@@ -0,0 +1,56 @@
+"""The fork sync acts on ONE upstream commit (review R6 m2).
+
+``_sync_with_upstream_if_needed`` fetches ``refs/remotes/upstream/main`` and fast-forwards to it. The
+short name ``upstream/main`` also names a LOCAL branch of that name (git resolves ``refs/heads``
+before ``refs/remotes``), so a count by short name and a merge by full ref could each see a
+different commit: the sync printed "1 behind" and merged 2 commits.
+"""
+
+from __future__ import annotations
+
+import os
+import subprocess
+from pathlib import Path
+
+from hermes_cli.update_cmd_git import _sync_with_upstream_if_needed
+
+
+def _env(home: Path) -> dict:
+ return {**os.environ, "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1", "HOME": str(home),
+ "GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "[email protected]",
+ "GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "[email protected]"}
+
+
+def _git(cwd: Path, *args: str) -> str:
+ return subprocess.run(["git", *args], cwd=cwd, check=True, capture_output=True, text=True,
+ encoding="utf-8", env=_env(cwd)).stdout.strip()
+
+
+def test_a_local_branch_named_upstream_main_never_changes_what_the_sync_counts_or_merges(
+ tmp_path, monkeypatch, capsys):
+ for name, value in _env(tmp_path).items():
+ if name.startswith("GIT_") or name == "HOME":
+ monkeypatch.setenv(name, value)
+ upstream = tmp_path / "upstream"
+ upstream.mkdir()
+ _git(upstream, "init", "-q", "-b", "main")
+ commits = []
+ for i in range(3):
+ (upstream / "f.txt").write_text(f"{i}\n", encoding="utf-8")
+ _git(upstream, "add", "f.txt")
+ _git(upstream, "commit", "-qm", f"c{i}")
+ commits.append(_git(upstream, "rev-parse", "HEAD"))
+ origin = tmp_path / "origin.git"
+ _git(tmp_path, "clone", "-q", "--bare", str(upstream), str(origin))
+ _git(origin, "update-ref", "refs/heads/main", commits[0])
+ clone = tmp_path / "clone"
+ _git(tmp_path, "clone", "-q", str(origin), str(clone))
+ _git(clone, "remote", "add", "upstream", str(upstream))
+ # The shadow: a local branch literally named upstream/main, one commit behind upstream.
+ _git(clone, "fetch", "-q", "upstream")
+ _git(clone, "branch", "upstream/main", commits[1])
+
+ assert _sync_with_upstream_if_needed(["git"], clone, assume_yes=True) is True
+ out = capsys.readouterr().out
+ assert "Fork is 2 commit(s) behind upstream" in out, out
+ assert _git(clone, "rev-parse", "HEAD") == commits[2]
diff --git a/tests/hermes_cli/test_update_target_identity.py b/tests/hermes_cli/test_update_target_identity.py
index 318b178f689..0b76375390d 100644
--- a/tests/hermes_cli/test_update_target_identity.py
+++ b/tests/hermes_cli/test_update_target_identity.py
@@ -123,10 +123,16 @@ def test_branch_update_uses_real_refs_and_completion_request(update_tree, monkey
run = subprocess.run
pushes = []
+ def _upstream_sha():
+ # The fork sync merges the commit it resolved from refs/remotes/upstream/main (m2).
+ found = run(['git', 'rev-parse', '-q', '--verify', 'refs/remotes/upstream/main'], cwd=t.clone,
+ capture_output=True, text=True, encoding='utf-8')
+ return found.stdout.strip() or '<no upstream>'
+
def fault(command, *args, **kwargs):
assert Path(command[0]).name.lower() in {'git', 'git.exe'} or command[0] == sys.executable, command
assert Path(kwargs['cwd']).resolve() in {t.clone, t.origin}, command
- if 'refs/remotes/upstream/main' in command: # the fork sync's local fast-forward
+ if 'merge' in command and _upstream_sha() in command: # the fork sync's local fast-forward
result = run(command, *args, **kwargs)
if case.startswith('fork-late'):
if case.endswith('wrong-branch'):