From 078871842dac9f90cf66b4126100266b06da18a7 Mon Sep 17 00:00:00 2001 From: agent-hackathon <18023767+tuyentran4992@users.noreply.github.com> Date: Fri, 16 Oct 2026 01:46:51 +0000 Subject: [PATCH] fix(init): stop naming a language from files inside dependency directories `init` fed the raw `git ls-files` output to language detection, so a single `.php` file inside a `node_modules` that `.gitignore` does not cover was enough to name php. The init path now drops `node_modules`, `vendor`, `site-packages`, `.venv` and `venv` before detection, while a PHP source outside those directories is still named php. --- src/habit_hooks/initialise.py | 14 +++- ...no_language_from_dependency_directories.py | 84 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tests/test_init_names_no_language_from_dependency_directories.py diff --git a/src/habit_hooks/initialise.py b/src/habit_hooks/initialise.py index c2f9b7b..79beefb 100644 --- a/src/habit_hooks/initialise.py +++ b/src/habit_hooks/initialise.py @@ -18,6 +18,10 @@ LANGUAGE_AGNOSTIC_PLUGIN = "generic" +DEPENDENCY_DIRECTORIES = frozenset( + {"node_modules", "vendor", "site-packages", ".venv", "venv"} +) + @frozen class Plan: @@ -42,8 +46,16 @@ def installs(self) -> tuple[str, ...]: ) +def _inside_dependency_directory(path: str) -> bool: + return any(part in DEPENDENCY_DIRECTORIES for part in path.split("/")[:-1]) + + def plan(project_dir: Path) -> Plan: - files = git_listing.project_files(project_dir) + files = [ + path + for path in git_listing.project_files(project_dir) + if not _inside_dependency_directory(path) + ] languages = tuple(used_languages(project_dir, files)) config = _project_config(project_dir) plugins = _plugins(languages, config) diff --git a/tests/test_init_names_no_language_from_dependency_directories.py b/tests/test_init_names_no_language_from_dependency_directories.py new file mode 100644 index 0000000..d3c9d70 --- /dev/null +++ b/tests/test_init_names_no_language_from_dependency_directories.py @@ -0,0 +1,84 @@ +from __future__ import annotations + +from pathlib import Path + +from git_repo import git +from habit_hooks.initialise import plan + + +def _tracking(project_dir: Path, files: dict[str, str]) -> None: + git(project_dir, "init", "-q", "-b", "main", ".") + for relative, body in files.items(): + path = project_dir / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(body, encoding="utf-8") + git(project_dir, "add", "-A") + + +def test_a_php_file_inside_node_modules_names_no_language(init_project: Path) -> None: + _tracking( + init_project, {"node_modules/legacy-lib/index.php": " None: + _tracking( + init_project, {"packages/app/node_modules/x/index.ts": "export const x = 1;\n"} + ) + + assert plan(init_project).languages == () + + +def test_a_python_file_inside_site_packages_names_no_language( + init_project: Path, +) -> None: + _tracking(init_project, {"site-packages/foo/mod.py": "x = 1\n"}) + + assert plan(init_project).languages == () + + +def test_a_php_file_inside_vendor_names_no_language(init_project: Path) -> None: + _tracking(init_project, {"vendor/legacy/index.php": " None: + _tracking(init_project, {"venv/lib/python3.12/site-packages/dep.py": "x = 1\n"}) + + assert plan(init_project).languages == () + + +def test_a_python_file_under_dot_venv_names_no_language(init_project: Path) -> None: + _tracking(init_project, {".venv/lib/dep.py": "x = 1\n"}) + + assert plan(init_project).languages == () + + +def test_real_php_source_still_names_php(init_project: Path) -> None: + _tracking(init_project, {"src/app.php": " None: + _tracking(init_project, {"src/vendorize.py": "x = 1\n"}) + + assert plan(init_project).languages == ("python",) + + +def test_a_project_without_dependency_directories_is_unchanged( + init_project: Path, +) -> None: + _tracking(init_project, {"src/app.py": "x = 1\n", "README.md": "# demo\n"}) + + assert plan(init_project).languages == ("python",)