From d967692c66ed4a7f561b6c94f66a3e98e1d01f47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=20Br=C3=BCgger?= Date: Thu, 5 Dec 2024 08:43:12 +0100 Subject: [PATCH] Relativize paths for inclusion and exclusion filters (cherry picked from commit 456a602db738400a93e528c4963221f967dd5282) --- .../ch/ergon/todochecker/TodoScanner.kt | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/ch/ergon/todochecker/TodoScanner.kt b/src/main/kotlin/ch/ergon/todochecker/TodoScanner.kt index 9d3559a..ffe926b 100644 --- a/src/main/kotlin/ch/ergon/todochecker/TodoScanner.kt +++ b/src/main/kotlin/ch/ergon/todochecker/TodoScanner.kt @@ -41,9 +41,10 @@ internal class TodoScanner internal constructor( */ fun scan(): Map> = Files.walk(directory).use { stream -> - stream.filter { path -> Files.isRegularFile(path) } - .filter(::notExcluded) - .filter(isIncluded().or(hasTextContent())) + stream + .filter { path -> Files.isRegularFile(path) } + .filter { path -> notExcluded(path, directory) } + .filter(isIncluded(directory).or(hasTextContent())) .flatMap { path -> getTodoForFile(path).entries.stream() } .collect( Collectors.groupingBy( @@ -56,21 +57,23 @@ internal class TodoScanner internal constructor( ) } - private fun notExcluded(file: Path): Boolean { + private fun notExcluded(file: Path, directory: Path): Boolean { + val relativizedPath = directory.relativize(file) val excluded = FileSystems.getDefault() .getPathMatcher(exclusions) - .matches(file) + .matches(relativizedPath) if (excluded) { - logger.info("Skipping file \"$file\": Excluded") + logger.info("Skipping file \"$relativizedPath\": Excluded") } return !excluded } - private fun isIncluded(): Predicate = + private fun isIncluded(directory: Path): Predicate = Predicate { path -> + val relativizedPath = directory.relativize(path) FileSystems.getDefault() .getPathMatcher(inclusions) - .matches(path) + .matches(relativizedPath) } private fun hasTextContent(): Predicate =