check_consistent.py: ignore .gitignored files (#8804)

This commit is contained in:
Alex Waygood
2022-09-28 17:02:27 +01:00
committed by GitHub
parent 133e2d860d
commit ab2fba9c83
4 changed files with 39 additions and 8 deletions

View File

@@ -6,6 +6,7 @@ from functools import cache
from pathlib import Path
from typing import NamedTuple
import pathspec # type: ignore[import]
import tomli
@@ -81,3 +82,21 @@ def get_all_testcase_directories() -> list[PackageInfo]:
if potential_testcase_dir.is_dir():
testcase_directories.append(PackageInfo(package_name, potential_testcase_dir))
return sorted(testcase_directories)
# ====================================================================
# Parsing .gitignore
# ====================================================================
@cache
def get_gitignore_spec() -> pathspec.PathSpec:
with open(".gitignore") as f:
return pathspec.PathSpec.from_lines("gitwildmatch", f.readlines())
def spec_matches_path(spec: pathspec.PathSpec, path: Path) -> bool:
normalized_path = path.as_posix()
if path.is_dir():
normalized_path += "/"
return spec.match_file(normalized_path) # type: ignore[no-any-return]