Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(doctor): fallback to environment-honouring Repo constructor for linked worktrees (#1931) (#1952)
Teddy100x738257a9d26d
diff --git a/packages/cli/src/repowise/cli/commands/doctor_cmd/_checks/core.py b/packages/cli/src/repowise/cli/commands/doctor_cmd/_checks/core.pyindex 2bf31250e..1ce7ad80f 100644--- a/packages/cli/src/repowise/cli/commands/doctor_cmd/_checks/core.py+++ b/packages/cli/src/repowise/cli/commands/doctor_cmd/_checks/core.py@@ -2,6 +2,7 @@from __future__ import annotations+import osfrom pathlib import Path as _DoctorPathfrom repowise.cli.helpers import db_configured, load_state, run_async@@ -115,11 +116,38 @@ def _events_config_check(repo_path: _DoctorPath) -> DoctorCheck | None:return _check("Events config", False, "; ".join(errors) + " (defaults used)")+def _env_repo_covers(repo_path: _DoctorPath) -> bool:+ """Whether the repo ``GIT_DIR`` / ``GIT_WORK_TREE`` name holds ``repo_path`` (#1931).++ A linked worktree or container can have a ``.git`` file the explicit-path+ constructor cannot follow. Without those variables ``Repo()`` searches up+ from the cwd, which says nothing about ``repo_path``.+ """+ if not (os.environ.get("GIT_DIR") or os.environ.get("GIT_WORK_TREE")):+ return False+ from repowise.core.git_repo import Repo++ found = Repo()+ try:+ work_tree = found.working_tree_dir+ finally:+ found.close()+ if work_tree is None:+ return False+ return _DoctorPath(repo_path).resolve().is_relative_to(_DoctorPath(work_tree).resolve())++def _git_check(repo_path: _DoctorPath) -> DoctorCheck:try:+ from git import InvalidGitRepositoryError, NoSuchPathError+from repowise.core.git_repo import Repo- Repo(repo_path, search_parent_directories=True).close()+ try:+ Repo(repo_path, search_parent_directories=True).close()+ except (InvalidGitRepositoryError, NoSuchPathError):+ if not _env_repo_covers(repo_path):+ raisereturn _check("Git repository", True, str(repo_path))except Exception:return _check("Git repository", False, "Not a git repo")diff --git a/tests/unit/cli/test_doctor_git_check.py b/tests/unit/cli/test_doctor_git_check.pynew file mode 100644index 000000000..9a9962d59--- /dev/null+++ b/tests/unit/cli/test_doctor_git_check.py@@ -0,0 +1,70 @@+"""Doctor's git row honours GIT_DIR / GIT_WORK_TREE without trusting the cwd (#1931).++In a linked worktree or a container the explicit-path constructor can fail+where the environment names the repo. The fallback must answer for+``repo_path``: ``Repo()`` with no variables set searches up from the cwd, so+a doctor run from inside any checkout would report a path that is not a repo+as healthy.+"""++from __future__ import annotations++import subprocess+from pathlib import Path++import pytest++from repowise.cli.commands.doctor_cmd._checks.core import _git_check+++def _git_repo(path: Path) -> Path:+ path.mkdir(parents=True, exist_ok=True)+ subprocess.run(["git", "init", "-q"], cwd=path, check=True)+ return path++[email protected](autouse=True)+def _no_git_env(monkeypatch: pytest.MonkeyPatch) -> None:+ for name in ("GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR"):+ monkeypatch.delenv(name, raising=False)+++def test_cwd_inside_another_repo_is_not_a_pass(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ other = _git_repo(tmp_path / "other")+ not_a_repo = tmp_path / "plain"+ not_a_repo.mkdir()+ monkeypatch.chdir(other)++ row = _git_check(not_a_repo)++ assert not row.ok+++def test_git_dir_naming_the_work_tree_passes(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ # The explicit constructor finds no .git under work_tree; the variables do.+ store = _git_repo(tmp_path / "store")+ work_tree = tmp_path / "checkout"+ work_tree.mkdir()+ monkeypatch.setenv("GIT_DIR", str(store / ".git"))+ monkeypatch.setenv("GIT_WORK_TREE", str(work_tree))++ row = _git_check(work_tree)++ assert row.ok+++def test_git_dir_for_a_different_tree_is_not_a_pass(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ store = _git_repo(tmp_path / "store")+ not_a_repo = tmp_path / "plain"+ not_a_repo.mkdir()+ monkeypatch.setenv("GIT_DIR", str(store / ".git"))++ row = _git_check(not_a_repo)++ assert not row.ok