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(cli): reject init --repos with no repo names (#4300)

Raghav Chamadiyaec24f1e9e208
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/_workspace_repos.py b/packages/cli/src/repowise/cli/commands/init_cmd/_workspace_repos.py
index 868c24f60..062f76641 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/_workspace_repos.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/_workspace_repos.py
@@ -28,8 +28,11 @@ def split_repo_names(raw: str | None) -> list[str]:
def check_repos_flag(repos: str | None, *, init_all: bool, is_workspace: bool) -> None:
"""Reject ``--repos`` where it cannot apply, before any indexing starts."""
- if not repos:
+ if repos is None:
return
+ # An empty value would otherwise fall through to indexing every repo.
+ if not split_repo_names(repos):
+ raise click.UsageError("--repos needs at least one repo name.")
if init_all:
raise click.UsageError("--repos and --all contradict each other. Pass one.")
if not is_workspace:
diff --git a/packages/cli/src/repowise/cli/ui/workspace_selection.py b/packages/cli/src/repowise/cli/ui/workspace_selection.py
index 78eb1ae81..4f79000d4 100644
--- a/packages/cli/src/repowise/cli/ui/workspace_selection.py
+++ b/packages/cli/src/repowise/cli/ui/workspace_selection.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+from pathlib import Path
from typing import Any
from rich.console import Console
@@ -139,20 +140,17 @@ def match_repo_names(repos: list[Any], names: list[str], root: Any = None) -> li
"""The repos *names* pick, in the order named.
A name matches a repo's alias, its directory name, or its path relative to
- *root* (the column the picker shows), case-insensitively. A name two repos
- share is ambiguous: the path tells them apart. Raises ``ValueError`` with a
- message that names every bad name and what would work instead.
+ *root* (the column the picker shows), case-insensitively. A filesystem path
+ (``./api`` or absolute, relative ones read from *root* first) that resolves
+ to a repo's folder matches it too. A name two repos share is ambiguous: the
+ path tells them apart. Raises ``ValueError`` with a message that names every
+ bad name and what would work instead.
"""
picked: list[Any] = []
seen: set[str] = set()
unknown: list[str] = []
for raw in names:
- name = raw.replace("\\", "/").strip("/").casefold()
- hits = [
- r
- for r in repos
- if name in (r.alias.casefold(), r.name.casefold(), display_path(r, root).casefold())
- ]
+ hits = _name_hits(repos, raw, root) or _path_hits(repos, raw, root)
if not hits:
unknown.append(raw)
elif len(hits) > 1:
@@ -167,6 +165,31 @@ def match_repo_names(repos: list[Any], names: list[str], root: Any = None) -> li
return picked
+def _name_hits(repos: list[Any], raw: str, root: Any) -> list[Any]:
+ """Repos whose alias, directory name or root-relative path is *raw*."""
+ name = raw.replace("\\", "/").strip("/").casefold()
+ return [
+ r
+ for r in repos
+ if name in (r.alias.casefold(), r.name.casefold(), display_path(r, root).casefold())
+ ]
+
+
+def _path_hits(repos: list[Any], raw: str, root: Any) -> list[Any]:
+ """Repos whose folder *raw* resolves to, a relative path read from *root* first."""
+ given = Path(raw).expanduser()
+ bases = [Path(root) / given] if root is not None and not given.is_absolute() else []
+ targets = {t for t in map(_resolved, [*bases, given]) if t is not None}
+ return [r for r in repos if _resolved(r.path) in targets]
+
+
+def _resolved(path: Path) -> Path | None:
+ try:
+ return path.resolve()
+ except (OSError, RuntimeError):
+ return None
+
+
def interactive_primary_select(
console: Console,
repos: list[Any],
diff --git a/tests/unit/cli/test_workspace_repo_selection.py b/tests/unit/cli/test_workspace_repo_selection.py
index c195e9437..398fbe13d 100644
--- a/tests/unit/cli/test_workspace_repo_selection.py
+++ b/tests/unit/cli/test_workspace_repo_selection.py
@@ -133,3 +133,48 @@ def test_repos_with_all_is_a_usage_error(tmp_path):
result = CliRunner().invoke(init_command, [str(tmp_path), "--repos", "x", "--all"])
assert result.exit_code == 2
assert "--repos and --all contradict each other" in result.output
+
+
[email protected]("value", ["", ",", " , "])
+def test_repos_with_no_names_is_a_usage_error(tmp_path, value):
+ from click.testing import CliRunner
+
+ from repowise.cli.commands.init_cmd import init_command
+
+ for name in ("repo-a", "repo-b"):
+ (tmp_path / name / ".git").mkdir(parents=True)
+ result = CliRunner().invoke(
+ init_command, [str(tmp_path), "--yes", "--no-editor-setup", "--repos", value]
+ )
+ assert result.exit_code == 2
+ assert "--repos needs at least one repo name" in result.output
+
+
+def test_a_path_to_a_repo_folder_matches_it(tmp_path, monkeypatch):
+ repos = [SimpleNamespace(path=tmp_path / n, name=n, alias=n) for n in ("repo-a", "repo-b")]
+ monkeypatch.chdir(tmp_path)
+ assert match_repo_names(repos, ["./repo-b"], tmp_path) == [repos[1]]
+ assert match_repo_names(repos, [str(tmp_path / "repo-a")], tmp_path) == [repos[0]]
+ with pytest.raises(ValueError, match=r"Unknown repo: \./nope"):
+ match_repo_names(repos, ["./nope"], tmp_path)
+
+
+def test_a_relative_path_is_read_from_the_workspace_root(tmp_path, monkeypatch):
+ root = tmp_path / "ws"
+ repos = [SimpleNamespace(path=root / n, name=n, alias=n) for n in ("repo-a", "repo-b")]
+ for repo in repos:
+ repo.path.mkdir(parents=True)
+ elsewhere = tmp_path / "elsewhere"
+ elsewhere.mkdir()
+ monkeypatch.chdir(elsewhere)
+ assert match_repo_names(repos, ["./repo-b"], root) == [repos[1]]
+
+
+def test_a_path_that_cannot_be_resolved_matches_nothing(tmp_path, monkeypatch):
+ from repowise.cli.ui import workspace_selection
+
+ def _fail(self, *a, **k):
+ raise OSError("unreadable")
+
+ monkeypatch.setattr(workspace_selection.Path, "resolve", _fail)
+ assert workspace_selection._resolved(tmp_path) is None