"""muse#54 item 8 -- `muse reflog anchor COMMIT [--json]`. Given a commit ID, search the HEAD log and every branch's reflog for entries that moved a ref TO that commit, reporting ref + @{N} index for each match. """ from __future__ import annotations import json import pathlib from tests.cli_test_helper import CliRunner from muse.core.types import fake_id, Manifest from muse.core.paths import muse_dir cli = None runner = CliRunner() def _env(root: pathlib.Path) -> Manifest: return {"MUSE_REPO_ROOT": str(root)} def _run(root: pathlib.Path, *args: str): return runner.invoke(cli, list(args), env=_env(root)) def _init_repo(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]: dot_muse = muse_dir(tmp_path) dot_muse.mkdir() repo_id = fake_id("repo") (dot_muse / "repo.json").write_text(json.dumps({ "repo_id": repo_id, "domain": "code", "default_branch": "main", "created_at": "2025-01-01T00:00:00+00:00", }), encoding="utf-8") (dot_muse / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8") (dot_muse / "refs" / "heads").mkdir(parents=True) (dot_muse / "snapshots").mkdir() (dot_muse / "commits").mkdir() (dot_muse / "objects").mkdir() return tmp_path, repo_id def _commit_file(root: pathlib.Path, content: str, message: str) -> str: (root / "f.txt").write_text(content) add = _run(root, "code", "add", "f.txt") assert add.exit_code == 0, add.output commit = _run(root, "commit", "-m", message, "--json") assert commit.exit_code == 0, commit.output return json.loads(commit.output)["commit_id"] class TestReflogAnchor: def test_finds_commit_on_head_log(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) c1 = _commit_file(root, "v1\n", "c1") c2 = _commit_file(root, "v2\n", "c2") result = _run(root, "reflog", "anchor", c2, "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert data["commit_id"] == c2 assert data["count"] >= 1 assert any(m["ref"] == "HEAD" and m["index"] == 0 for m in data["matches"]) def test_finds_commit_on_named_branch_log(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) c1 = _commit_file(root, "v1\n", "c1") result = _run(root, "reflog", "anchor", c1, "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert any(m["ref"] == "refs/heads/main" for m in data["matches"]) def test_unmatched_commit_returns_empty(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) _commit_file(root, "v1\n", "c1") fake_commit = "sha256:" + "0" * 64 result = _run(root, "reflog", "anchor", fake_commit, "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert data["count"] == 0 assert data["matches"] == [] def test_accepts_bare_hex_without_prefix(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) c1 = _commit_file(root, "v1\n", "c1") bare_hex = c1.split(":", 1)[1] result = _run(root, "reflog", "anchor", bare_hex, "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert data["commit_id"] == c1 assert data["count"] >= 1 def test_second_commit_not_anchored_at_first_commits_index(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) c1 = _commit_file(root, "v1\n", "c1") c2 = _commit_file(root, "v2\n", "c2") result = _run(root, "reflog", "anchor", c1, "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) # c1 should be found, but not at the newest (@{0}) HEAD index -- # that belongs to c2. head_matches = [m for m in data["matches"] if m["ref"] == "HEAD"] assert all(m["index"] != 0 for m in head_matches)