Skip to content

Commit c0dce7f

Browse files
authored
fix: hide pre-commit checks if lefthook is used (#702)
Signed-off-by: Henry Schreiner <[email protected]>
1 parent c3e0a87 commit c0dce7f

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ messages_control.disable = [
159159
]
160160

161161

162+
[tool.ruff]
163+
show-fixes = true
164+
extend-exclude = ["\\{\\{cookiecutter.project_name\\}\\}"]
165+
162166
[tool.ruff.lint]
163167
extend-select = [
164168
"ARG", # flake8-unused-arguments

src/sp_repo_review/checks/precommit.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,14 @@ def check(precommit: dict[str, Any]) -> bool:
245245
return "autoupdate_schedule" in precommit.get("ci", {})
246246

247247

248-
def repo_review_checks() -> dict[str, PreCommit]:
248+
def repo_review_checks(
249+
list_all: bool = True,
250+
root: Traversable | None = None,
251+
) -> dict[str, PreCommit]:
252+
if root and not list_all:
253+
precommit_path = root.joinpath(".pre-commit-config.yaml")
254+
lefthook_path = root.joinpath("lefthook.yml")
255+
if not precommit_path.is_file() and lefthook_path.is_file():
256+
return {}
257+
249258
return {p.__name__: p() for p in PreCommit.__subclasses__()}

tests/test_precommit.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
from pathlib import Path
2+
13
import pytest
24
import yaml
35
from repo_review.testing import compute_check
46

7+
from sp_repo_review.checks.precommit import repo_review_checks
8+
59

610
@pytest.fixture(params=["ruff", "ruff-check"])
711
def ruff_check(request: pytest.FixtureRequest) -> str:
@@ -283,3 +287,17 @@ def test_pc903_no_msg():
283287
res = compute_check("PC903", precommit=precommit)
284288
assert not res.result
285289
assert "autoupdate_schedule" in res.err_msg
290+
291+
292+
def test_repo_review_checks_skips_with_lefthook_only(tmp_path: Path) -> None:
293+
"""PreCommit checks should be omitted if only lefthook.yml is present.
294+
295+
When a repository uses `lefthook.yml` and does not have a
296+
`.pre-commit-config.yaml`, `repo_review_checks` should return an empty
297+
mapping when `list_all=False` indicating the pre-commit family is skipped.
298+
"""
299+
# Create only a lefthook configuration
300+
(tmp_path / "lefthook.yml").write_text("hooks:\n", encoding="utf-8")
301+
302+
checks = repo_review_checks(list_all=False, root=tmp_path)
303+
assert checks == {}

0 commit comments

Comments
 (0)