Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(hook): filter workspace targets and report failures (#2478)

spark172017eca2e1
diff --git a/packages/cli/src/repowise/cli/commands/hook_cmd.py b/packages/cli/src/repowise/cli/commands/hook_cmd.py
index ae58453fc..ed7456f81 100644
--- a/packages/cli/src/repowise/cli/commands/hook_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/hook_cmd.py
@@ -2,6 +2,9 @@
from __future__ import annotations
+from collections.abc import Callable
+from pathlib import Path
+
import click
from repowise.cli.helpers import (
@@ -69,17 +72,7 @@ def hook_install(path: str | None, workspace: bool, no_workspace: bool) -> None:
from repowise.cli.hooks import install
target = _hook_target(path, workspace, no_workspace)
-
- if target.is_workspace:
- assert target.ws_root is not None and target.ws_config is not None
- for entry in target.ws_config.repos:
- abs_path = (target.ws_root / entry.path).resolve()
- result = install(abs_path)
- console.print(f" {entry.alias}: [green]{result}[/green]")
- else:
- assert target.repo_path is not None
- result = install(target.repo_path)
- console.print(f"Post-commit hook: [green]{result}[/green]")
+ _run_post_commit_action(target, install, action="installed")
@hook_group.command("uninstall")
@@ -102,17 +95,29 @@ def hook_uninstall(path: str | None, workspace: bool, no_workspace: bool) -> Non
from repowise.cli.hooks import uninstall
target = _hook_target(path, workspace, no_workspace)
+ _run_post_commit_action(target, uninstall, action="uninstalled")
- if target.is_workspace:
- assert target.ws_root is not None and target.ws_config is not None
- for entry in target.ws_config.repos:
- abs_path = (target.ws_root / entry.path).resolve()
- result = uninstall(abs_path)
- console.print(f" {entry.alias}: {result}")
- else:
- assert target.repo_path is not None
- result = uninstall(target.repo_path)
- console.print(f"Post-commit hook: {result}")
+
+def _run_post_commit_action(target, run: Callable[[Path], str], *, action: str) -> None:
+ """Run a post-commit hook action across indexed targets."""
+ entries = _target_repo_entries(target)
+ if not entries:
+ raise click.ClickException(
+ "No indexed repositories found. Run `repowise init` before managing hooks."
+ )
+
+ succeeded = 0
+ for alias, repo_path in entries:
+ result = run(repo_path)
+ label = f" {alias}" if target.is_workspace else "Post-commit hook"
+ if result.startswith("not "):
+ console.print(f"{label}: [yellow]{result}[/yellow]")
+ continue
+ succeeded += 1
+ console.print(f"{label}: [green]{result}[/green]")
+
+ if not succeeded:
+ raise click.ClickException(f"No post-commit hooks were {action}.")
@hook_group.group("rewrite")
@@ -134,17 +139,26 @@ def rewrite_group() -> None:
"""
-def _target_repo_paths(target) -> list:
- """The repo paths a hook subcommand should act on (repowise repos only)."""
+def _target_repo_entries(target) -> list[tuple[str, Path]]:
+ """The indexed aliases and paths a hook subcommand should act on."""
if target.is_workspace:
assert target.ws_root is not None and target.ws_config is not None
return [
- (target.ws_root / entry.path).resolve()
+ (entry.alias, (target.ws_root / entry.path).resolve())
for entry in target.ws_config.repos
if ((target.ws_root / entry.path).resolve() / ".repowise").is_dir()
]
assert target.repo_path is not None
- return [target.repo_path] if (target.repo_path / ".repowise").is_dir() else []
+ return (
+ [(target.repo_path.name, target.repo_path)]
+ if (target.repo_path / ".repowise").is_dir()
+ else []
+ )
+
+
+def _target_repo_paths(target) -> list[Path]:
+ """The repo paths a hook subcommand should act on (repowise repos only)."""
+ return [repo_path for _, repo_path in _target_repo_entries(target)]
def _print_rewrite_hook_status(label: str, status) -> None:
diff --git a/tests/unit/cli/test_hook_cmd.py b/tests/unit/cli/test_hook_cmd.py
new file mode 100644
index 000000000..6bde96f57
--- /dev/null
+++ b/tests/unit/cli/test_hook_cmd.py
@@ -0,0 +1,119 @@
+"""Workspace behavior for the post-commit hook install/uninstall commands."""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+from click.testing import CliRunner
+
+from repowise.cli.commands import hook_cmd
+from repowise.cli.commands.hook_cmd import hook_group
+
+
+class _Console:
+ def __init__(self) -> None:
+ self.lines: list[str] = []
+
+ def print(self, *args, **_kwargs) -> None:
+ self.lines.append(" ".join(str(arg) for arg in args))
+
+ @property
+ def text(self) -> str:
+ return "\n".join(self.lines)
+
+
+def workspace(tmp_path: Path) -> Path:
+ for name in ("backend", "frontend"):
+ repo = tmp_path / name
+ repo.mkdir()
+ (repo / ".repowise").mkdir()
+ (tmp_path / "unindexed").mkdir()
+ (tmp_path / ".repowise-workspace.yaml").write_text(
+ "version: 1\n"
+ "default_repo: backend\n"
+ "repos:\n"
+ " - path: backend\n"
+ " alias: backend\n"
+ " is_primary: true\n"
+ " - path: frontend\n"
+ " alias: frontend\n"
+ " - path: unindexed\n"
+ " alias: unindexed\n"
+ )
+ return tmp_path
+
+
+def test_install_skips_unindexed_repos_and_colours_failures(
+ workspace: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ calls: list[Path] = []
+
+ def _install(repo_path: Path) -> str:
+ calls.append(repo_path)
+ if repo_path.name == "backend":
+ return "installed"
+ return "not a git repository"
+
+ console = _Console()
+ monkeypatch.setattr(hook_cmd, "console", console)
+ monkeypatch.setattr("repowise.cli.hooks.install", _install)
+
+ result = CliRunner().invoke(hook_group, ["install", str(workspace)])
+
+ assert result.exit_code == 0
+ assert calls == [workspace / "backend", workspace / "frontend"]
+ assert " backend: [green]installed[/green]" in console.text
+ assert " frontend: [yellow]not a git repository[/yellow]" in console.text
+ assert " unindexed:" not in console.text
+
+
+def test_install_exits_nonzero_when_no_indexed_target_succeeds(
+ workspace: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ console = _Console()
+ monkeypatch.setattr(hook_cmd, "console", console)
+ monkeypatch.setattr("repowise.cli.hooks.install", lambda _repo_path: "not a git repository")
+
+ result = CliRunner().invoke(hook_group, ["install", str(workspace)])
+
+ assert result.exit_code != 0
+ assert "No post-commit hooks were installed." in result.output
+ assert "[green]" not in console.text
+
+
+def test_uninstall_uses_the_same_indexed_targets_and_failure_colour(
+ workspace: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ calls: list[Path] = []
+
+ def _uninstall(repo_path: Path) -> str:
+ calls.append(repo_path)
+ if repo_path.name == "backend":
+ return "removed"
+ return "not a git repository"
+
+ console = _Console()
+ monkeypatch.setattr(hook_cmd, "console", console)
+ monkeypatch.setattr("repowise.cli.hooks.uninstall", _uninstall)
+
+ result = CliRunner().invoke(hook_group, ["uninstall", str(workspace)])
+
+ assert result.exit_code == 0
+ assert calls == [workspace / "backend", workspace / "frontend"]
+ assert " backend: [green]removed[/green]" in console.text
+ assert " frontend: [yellow]not a git repository[/yellow]" in console.text
+ assert " unindexed:" not in console.text
+
+
+def test_uninstall_exits_nonzero_when_no_indexed_target_succeeds(
+ workspace: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setattr(hook_cmd, "console", _Console())
+ monkeypatch.setattr("repowise.cli.hooks.uninstall", lambda _repo_path: "not a git repository")
+
+ result = CliRunner().invoke(hook_group, ["uninstall", str(workspace)])
+
+ assert result.exit_code != 0
+ assert "No post-commit hooks were uninstalled." in result.output