"""muse#54 item 2 -- `muse reflog --format oneline` / `--format patch`. Machine-parseable one-line output, and a patch format that follows each entry's oneline header with the diff it introduced. """ 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 TestReflogFormatOneline: def test_oneline_one_row_per_entry(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", "--format", "oneline") assert result.exit_code == 0, result.output lines = [l for l in result.output.splitlines() if l.strip()] assert len(lines) == 2 assert lines[0].startswith("@{0} " + c2) assert "c2" in lines[0] assert lines[1].startswith("@{1} " + c1) def test_oneline_mutually_exclusive_with_json(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) _commit_file(root, "v1\n", "c1") result = _run(root, "reflog", "--format", "oneline", "--json") assert result.exit_code == 1 class TestReflogFormatPatch: def test_patch_shows_diff_after_header(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) _commit_file(root, "v1\n", "c1") c2 = _commit_file(root, "v2\n", "c2") result = _run(root, "reflog", "--format", "patch", "--limit", "1") assert result.exit_code == 0, result.output assert result.output.startswith("@{0} " + c2) assert "f.txt" in result.output def test_patch_initial_entry_has_no_diff_crash(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) _commit_file(root, "v1\n", "c1") result = _run(root, "reflog", "--format", "patch") assert result.exit_code == 0, result.output assert "initial entry" in result.output