Skip to content

Commit ca1ffa9

Browse files
committed
perf(model): Filter down to relevant files before getting the main license
As an alternative to adding caching, which also included lookups for irrelevant files, use another approach to only look for relevant files in the first place. As only configured license file patterns can contribute to the main license, add a preceding step to only keep findings from those files before determining the main license. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 3a80c89 commit ca1ffa9

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

model/src/main/kotlin/licenses/ResolvedLicenseInfo.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.ossreviewtoolkit.model.config.CopyrightGarbage
2828
import org.ossreviewtoolkit.model.config.LicenseFilePatterns
2929
import org.ossreviewtoolkit.model.config.PathExclude
3030
import org.ossreviewtoolkit.model.utils.PathLicenseMatcher
31+
import org.ossreviewtoolkit.utils.common.FileMatcher
3132
import org.ossreviewtoolkit.utils.ort.CopyrightStatementsProcessor
3233
import org.ossreviewtoolkit.utils.spdx.SpdxExpression
3334
import org.ossreviewtoolkit.utils.spdx.SpdxLicenseChoice
@@ -83,12 +84,20 @@ data class ResolvedLicenseInfo(
8384
* in any of the configured [LicenseFilePatterns] matched against the root path of the package (or project).
8485
*/
8586
fun mainLicense(): SpdxExpression? {
86-
val licenseMatcher = PathLicenseMatcher(LicenseFilePatterns.getInstance())
87-
val licensePaths = flatMap { resolvedLicense ->
87+
val licenseFilePatterns = LicenseFilePatterns.getInstance()
88+
val fileMatcher = FileMatcher(licenseFilePatterns.allLicenseFilenames, ignoreCase = true)
89+
val licenseMatcher = PathLicenseMatcher(licenseFilePatterns)
90+
91+
val relevantResolvedLicenses = mapNotNull { resolvedLicense ->
92+
val locations = resolvedLicense.locations.filterTo(mutableSetOf()) { fileMatcher.matches(it.location.path) }
93+
if (locations.isNotEmpty()) resolvedLicense.copy(locations = locations) else null
94+
}
95+
96+
val licensePaths = relevantResolvedLicenses.flatMap { resolvedLicense ->
8897
resolvedLicense.locations.map { it.location.path }
8998
}
9099

91-
val detectedLicenses = filterTo(mutableSetOf()) { resolvedLicense ->
100+
val detectedLicenses = relevantResolvedLicenses.filterTo(mutableSetOf()) { resolvedLicense ->
92101
resolvedLicense.locations.any {
93102
val rootPath = (it.provenance as? RepositoryProvenance)?.vcsInfo?.path.orEmpty()
94103

0 commit comments

Comments
 (0)