Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor file matcher improvements #9886

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions evaluator/src/main/kotlin/ProjectSourceRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ open class ProjectSourceRule(
fun projectSourceFindDirectories(vararg patterns: String): List<File> =
projectSourcesDir.walkBottomUp().filterTo(mutableListOf()) {
val path = it.relativeTo(projectSourcesDir).invariantSeparatorsPath
it.isDirectory && FileMatcher.match(patterns.asList(), path)
it.isDirectory && FileMatcher.matches(patterns.asList(), path)
}

/**
Expand All @@ -79,15 +79,15 @@ open class ProjectSourceRule(
fun projectSourceFindFiles(vararg patterns: String): List<File> =
projectSourcesDir.walkBottomUp().filterTo(mutableListOf()) {
val path = it.relativeTo(projectSourcesDir).invariantSeparatorsPath
it.isFile && FileMatcher.match(patterns.asList(), path)
it.isFile && FileMatcher.matches(patterns.asList(), path)
}

/**
* Return the detected licenses for any file matching the given [glob expressions][patterns].
*/
fun projectSourceGetDetectedLicensesByFilePath(vararg patterns: String): Map<String, Set<String>> =
detectedLicensesForFilePath.filter { (filePath, _) ->
FileMatcher.match(patterns.asList(), filePath)
FileMatcher.matches(patterns.asList(), filePath)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ internal class ListLicensesCommand : OrtHelperCommand(
!offendingOnly || license.decompose().any { it in violatedRulesByLicense }
}.mapValues { (license, locations) ->
locations.filter { location ->
val isAllowedFile = fileAllowList.isEmpty() || FileMatcher.match(fileAllowList, location.path)
val isAllowedFile = fileAllowList.isEmpty() || FileMatcher.matches(fileAllowList, location.path)

val isIncluded = !omitExcluded || !isPathExcluded(provenance, location.path) ||
ignoreExcludedRuleIds.intersect(violatedRulesByLicense[license].orEmpty()).isNotEmpty()
Expand Down
4 changes: 2 additions & 2 deletions helper-cli/src/main/kotlin/utils/PathExcludeGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal object PathExcludeGenerator {

dirs.forEach { dir ->
val (_, reason) = PATH_EXCLUDES_REASON_FOR_DIR_NAME.find { (pattern, _) ->
FileMatcher.match(pattern, File(dir).name, ignoreCase = true)
FileMatcher.matches(pattern, File(dir).name, ignoreCase = true)
} ?: return@forEach

dirsToExclude += dir to reason
Expand Down Expand Up @@ -101,7 +101,7 @@ internal object PathExcludeGenerator {

internal fun createExcludePatterns(filenamePattern: String, filePaths: Collection<String>): Set<String> {
val matchingFiles = filePaths.mapNotNull { filePath ->
File(filePath).takeIf { FileMatcher.match(filenamePattern, it.name) }
File(filePath).takeIf { FileMatcher.matches(filenamePattern, it.name) }
}.ifEmpty {
return emptySet()
}
Expand Down
2 changes: 1 addition & 1 deletion model/src/main/kotlin/config/PathExclude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ data class PathExclude(
* Return true if and only if this [PathExclude] matches the given [path].
*/
fun matches(path: String) =
FileMatcher.match(
FileMatcher.matches(
pattern = pattern.removePrefix("./"),
path = path
)
Expand Down
2 changes: 1 addition & 1 deletion model/src/main/kotlin/utils/FindingCurationMatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FindingCurationMatcher {
curation: LicenseFindingCuration,
relativeFindingPath: String
): Boolean =
FileMatcher.match(
FileMatcher.matches(
pattern = curation.path,
path = finding.location.prependedPath(relativeFindingPath)
)
Expand Down
10 changes: 5 additions & 5 deletions utils/common/src/main/kotlin/FileMatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ package org.ossreviewtoolkit.utils.common
import org.springframework.util.AntPathMatcher

/**
* A class to determine whether paths are matched by globs. It can either be used via its static [match] functions, or
* (in case different paths have to be matched against the same patterns in the same way over and over again) by
* instantiating it with fixed [patterns], optionally [ignoring case][ignoreCase].
* A class to determine whether paths are matched by globs. It can either be used via its static
* [match][FileMatcher.Companion.matches] functions, or (in case different paths have to be matched against the same
* patterns in the same way over and over again) by instantiating it with fixed [patterns], optionally ignoring case.
*/
class FileMatcher(
/**
Expand All @@ -47,14 +47,14 @@ class FileMatcher(
* Return true if [path] is matched by [pattern], false otherwise. The [path] must use '/' as separators, if it
* contains any.
*/
fun match(pattern: String, path: String, ignoreCase: Boolean = false) =
fun matches(pattern: String, path: String, ignoreCase: Boolean = false) =
if (ignoreCase) matchCaseInsensitive(pattern, path) else matchCaseSensitive(pattern, path)

/**
* Return true if [path] is matched by any of [patterns], false otherwise. The [path] must use '/' as
* separators, if it contains any.
*/
fun match(patterns: Collection<String>, path: String, ignoreCase: Boolean = false): Boolean {
fun matches(patterns: Collection<String>, path: String, ignoreCase: Boolean = false): Boolean {
// Only decide once for all patterns which function to call.
val match = if (ignoreCase) matchCaseInsensitive else matchCaseSensitive

Expand Down
Loading